Javascript自动播放声音文件,单击其中一个按钮时停止

Javascript autoplay soundfile, stop when one of the buttons is clicked

本文关键字:一个 按钮 播放声音 文件 单击 Javascript      更新时间:2024-06-01

所以我有一些按钮,人们可以按下这些按钮来收听音频文件。当然,音频文件不能"重叠",因为那样会一团糟。这是可行的,但现在我希望其中一个文件在网站加载时自动播放,同时保持每当我按下另一个按钮时它就会停止的事实。

这就是我目前所掌握的:

头:

<script language="javascript" type="text/javascript">
 function playSound(soundfile) {
 document.getElementById("dummy").innerHTML=
 "<embed src='""+soundfile+"'" hidden='"true'" autostart='"false'" loop='"true'" />";
 }
 </script>

正文:

<span id="dummy"></span>
<p style="text-align:center;">
<a href="#" onclick="playSound('Sound1.mp3');"> 
<img class="alignnone" width="128" img src="Img1.png" ></a>
<a href="#" onclick="playSound('Sound2.mp3');"> 
<img class="alignnone" width="128" img src="Img2.png"  ></a>
<a href="#" onclick="playSound('Sound3.mp3');"> 
<img class="alignnone" width="128" img src="Img3.png"  ></a>
</p>

试试这个jquery解决方案:

http://archive.plugins.jquery.com/project/sound_plugin

我在反应时间测试中成功地使用了这个,每次测试前我都必须播放一个声音。