简体   繁体   中英

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM