Play Sound When message received

前端 未结 4 1401
误落风尘
误落风尘 2020-12-07 06:30

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

  

        
相关标签:
4条回答
  • 2020-12-07 06:48

    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.

    0 讨论(0)
  • 2020-12-07 06:55

    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" />
    
    0 讨论(0)
  • 2020-12-07 07:08

    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.

    0 讨论(0)
  • 2020-12-07 07:12

    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">
    
    0 讨论(0)
提交回复
热议问题