Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing sound notifications using Javascript?

How can I do that, so whenever a user clicks a link we play a sound? Using javascript and jquery here.

like image 282
Ali Avatar asked Jan 16 '09 10:01

Ali


People also ask

How to add Notification sound using JavaScript?

We can play a notification sound on websites with JavaScript by creating an audio player object with the Audio constructor. We create the playSound function that takes the audio url . And we pass the url into the Audio constructor to create the audio player object.

How do I play sound from a website?

An easy way to embed audio on a website is by using a sound hosting site, such as SoundCloud or Mixcloud. All you need to do is upload the file and receive an HTML embed code. Then copy and paste the embed code into the web page's code or WYSIWYG site editor. This works for most CMS platforms and website builders.

Can you add sound in JavaScript?

The simplest way to add sound is through Javascript's Audio() constructor. It takes an argument of a string that is either the local or remote file path. Declaring this as a variable allows you to then call the play() method which starts playing the current audio.


2 Answers

Use this plugin: https://github.com/admsev/jquery-play-sound

$.playSound('http://example.org/sound.mp3'); 
like image 61
Alexander Manzyuk Avatar answered Sep 21 '22 09:09

Alexander Manzyuk


Put an <audio> element on your page.
Get your audio element and call the play() method:

document.getElementById('yourAudioTag').play(); 

Check out this example: http://www.storiesinflight.com/html5/audio.html

This site uncovers some of the other cool things you can do such as load(), pause(), and a few other properties of the audio element.

When exactly you want to play this audio element is up to you. Read the text of the button and compare it to "no" if you like.

Alternatively

http://www.schillmania.com/projects/soundmanager2/

SoundManager 2 provides a easy to use API that allows sound to be played in any modern browser, including IE 6+. If the browser doesn't support HTML5, then it gets help from flash. If you want stricly HTML5 and no flash, there's a setting for that, preferFlash=false

It supports 100% Flash-free audio on iPad, iPhone (iOS4) and other HTML5-enabled devices + browsers

Use is as simple as:

<script src="soundmanager2.js"></script> <script> // where to find flash SWFs, if needed... soundManager.url = '/path/to/swf-files/';  soundManager.onready(function() {     soundManager.createSound({         id: 'mySound',         url: '/path/to/an.mp3'     });      // ...and play it     soundManager.play('mySound'); }); 

Here's a demo of it in action: http://www.schillmania.com/projects/soundmanager2/demo/christmas-lights/

like image 36
Joris Van Regemortel Avatar answered Sep 20 '22 09:09

Joris Van Regemortel