简体   繁体   English

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

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

So I've got some buttons, which people can press to hear audio files. 所以我有一些按钮,人们可以按这些按钮来收听音频文件。 Of course, the audio files can't "overlap" because that would be a mess. 当然,音频文件不能“重叠”,因为那样会很混乱。 This works, but now I want one of the files to play automatically when the website loads, still maintaining the fact that it will stop whenever I press another button. 这可行,但是现在我希望其中一个文件在网站加载时自动播放,但仍然保持这样的事实,只要我按另一个按钮,它就会停止。

This is what I've got so far: 到目前为止,这是我得到的:

Head: 头:

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

Body: 身体:

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

Try this jquery solution: 试试这个jQuery解决方案:

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

I used this successfully in a reaction time test where I had to play a sound just before each test. 我在反应时间测试中成功使用了此功能,在每次测试前都必须播放声音。

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

相关问题 手风琴菜单 - 单击一个按钮时如何停止所有按钮打开? - Accordion menu - how to stop all buttons opening when one is clicked? 使用angularjs单击一个按钮时禁用2个按钮 - Disable 2 buttons when one is clicked using angularjs 单击两个或多个按钮时弹出 Javascript - Javascript popup when two or more buttons clicked 单击一个按钮时禁用其余按钮 - Disable remaining buttons when one is clicked Javascript:当所有按钮都被点击时 function - Javascript: When all buttons are clicked function 为什么我的 javascript 上的按钮在单击时没有响应? - Why are my buttons on javascript not responding when clicked? 单击其他按钮时隐藏一种形式(两个按钮,两种形式)(PHP / HTML / Javascript) - Hide one form when other button is clicked on (two buttons, two forms) (PHP/HTML/Javascript) 有没有办法在使用 javascript 单击 DOM 中的许多按钮列表中识别一个按钮的索引? - Is there a way to identify the index of one button among a list of many buttons in the DOM when it is clicked using javascript? 单击按钮时触发 javascript function 并在再次单击时停止 - Trigger a javascript function when button is clicked and stop when clicked again 如何在不使用Jquery的情况下在javascript中仅单击一个按钮时禁用两个按钮 - How to Disable two buttons when only one is clicked in javascript without the use of Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM