javascript - Play Sound When message received - Stack Overflow

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

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.

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.

Share Improve this question edited Mar 18, 2013 at 17:51 Matt 5,4281 gold badge34 silver badges59 bronze badges asked Mar 18, 2013 at 17:47 Azam AlviAzam Alvi 7,0558 gold badges67 silver badges89 bronze badges 2
  • try using the html5 audio api instead, and as a fall back use flash. – Ibu Commented Mar 18, 2013 at 17:49
  • i also use <audio> tag bt same error – Azam Alvi Commented Mar 18, 2013 at 17:54
Add a ment  | 

5 Answers 5

Reset to default 9

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.

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" />

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.

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">
$("#elementid").trigger("click"); //<-- just to allow browser to play sound
clickSound.play();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743730403a4497335.html

相关推荐

  • javascript - Play Sound When message received - Stack Overflow

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

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信