简体   繁体   English

收到消息后播放声音

[英]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. 但是问题在于它在Godzilla / IE中无法播放声音,而在Chrome中无法播放。 Is there any way to solve this without adding any additional plugin (Mozilla/JQuery). 有什么方法可以解决此问题而无需添加任何其他插件(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. chrome中还有一个问题,当播放声音时,主窗口的滚动条会从其位置移动。 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 试试这个,我认为这是任何人都没有见过的最简单的解决方案... :)您只是做一件事,应该将beep.mp3转换为beep.wav因为某些浏览器不了解mp3,因此您应该将其转换为WAV,然后只使用这三行代码

 <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. 这将在打开/重新加载页面时播放声音,您可以根据需要进行设置,并且还需要js 1.6或更高版本。 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')来解决

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

and set any div . 并设置任何div

Try following (Not tested) 尝试关注(未测试)

Keep the sound file all ready embedded in the code and use javascript function to play it... 将声音文件准备好嵌入到代码中,并使用javascript函数播放...

<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. 如果不想创建多个音频元素,可以尝试将currentTime属性设置为0,以便音频文件重新开始。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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