Azam Alvi
Azam Alvi

Reputation: 7055

Play Sound When message received

I want to play sound when receiving message, so i searched it and found many solution but the simplest solution that i found is

  <script>  
       $('body').append('<embed src="beep.mp3" autostart="true" hidden="true">');
  </script>

but the problem is that it does not play sound in Godzilla/IE and play in Chrome. Is there any way to solve this without adding any additional plugin (Mozilla/JQuery). And there is also one problem in chrome that when sound play it's scroll bar of main window moves from it's position. My main problem is playing sound so it's priority is first. anyone knows it solution then plz share with us thanks.

Upvotes: 0

Views: 7537

Answers (5)

mana
mana

Reputation: 1239

$("#elementid").trigger("click"); //<-- just to allow browser to play sound
clickSound.play();

Upvotes: 0

user2166307
user2166307

Reputation:

Open this link: Play Sound without browser plugin.

In this link, you can get code to solve your problem. And the second problem may be solved by removing $('body')

$('body').append('<embed src="beep.mp3" autostart="true" hidden="true">');

and set any div.

Upvotes: 1

user2081972
user2081972

Reputation:

Try this one, i think it is the simplest solution that anyone ever seen...:) you just do one thing that you should convert your beep.mp3 to beep.wav because some browsers dont understand mp3 so you should just convert it to wav and then use this only 3 lines of code

 <script>  
     var aSound = document.createElement('audio');
     aSound.setAttribute('src', 'beep.wav');
     aSound.play();
 </script>  

this will play sound on when page is open/reload you can set it as you want, and one more thing js 1.6 or greater is required. hope this will solve your both problem.

Upvotes: 9

Steven V
Steven V

Reputation: 16585

If you don't want to create multiple audio elements, you can try setting the currentTime property to 0, so the audio file starts over.

<script>
    function play() {
        var sound = document.getElementById("audio");
        sound.currentTime = 0;
        sound.play();
    }
</script>

<audio src="success.wav" autostart="false" width="0" height="0" id="audio" />

Upvotes: 2

Jay Bhatt
Jay Bhatt

Reputation: 5651

Try following (Not tested)

Keep the sound file all ready embedded in the code and use javascript function to play it...

<script>
    function PlaySound(soundObj) {
        var sound = document.getElementById(soundObj);
        sound.Play();
    }
</script>

<embed src="success.wav" autostart="false" width="0" height="0" id="sound1" enablejavascript="true">

Upvotes: 0

Related Questions