在 javascript 数组中播放音频文件时遇到问题
Trouble playing audio files in javascript array
>我正在尝试创建一个按钮,单击该按钮时播放音频文件,然后再次单击时播放数组中的下一个文件。我让它播放,但是当我第二次单击它时,它会播放第一个文件和第二个文件。当我第三次按下它时,它会播放第一、第二和第三。似乎我需要重置某些内容或其他内容才能清除页面的前两个轨道。这是我所拥有的:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script language="javascript" type="text/javascript">
audio = new Array('audio1.mp3','audio2.mp3','audio3.mp3');
index = 0;
function playSound() {
if(index<3){
document.getElementById("start").innerHTML=document.getElementById("start").innerHTML +
"<embed src='""+audio[index]+"'" hidden='"true'" autostart='"true'" loop='"false'" />";
index++;
}
else{ index = 0;
document.getElementById("start").innerHTML=document.getElementById("start").innerHTML +
"<embed src='""+audio[index]+"'" hidden='"true'" autostart='"true'" loop='"false'" />";
index++;
}
}
</script>
<button id="start" type = "button" onclick="playSound();">Start</button>
</body>
</html>
function playSound() {
if(index>=3){
index = 0;
}
document.getElementById("sound").innerHTML=
"<embed src='""+audio[index]+"'" hidden='"true'" autostart='"true'" loop='"false'" />";
index++;
}
并添加一个div(您也可以使用css隐藏),我认为这是比将html添加到按钮更好的方法。
<button id="start" type = "button" onclick="playSound();">Start</button>
<div id="sound" style="display:none"></div>
您不断添加越来越多的<embed>
元素,但从未删除任何元素。
不要将它们追加到按钮,而是将它们追加到容器,然后在添加另一个容器之前删除它们。
相关文章:
- 如何播放部分音频文件
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- Ruby on Rails 4:在 Rails Web 应用程序中添加 Javascript 文件时遇到问题
- Java 脚本:递归地遍历关联的 HTML 文件的 Dom,并打印遇到的元素类型
- 在 javascript 数组中播放音频文件时遇到问题
- 在 Node.js 中使用 fs.readdir() 列出文件时遇到问题
- 将现有模型文件移动到Require.js中时遇到问题
- 在codeigniter框架中添加css和js文件时遇到麻烦
- 访问动态生成的javascript文件时遇到问题
- 从另一个文件"updateDB调用函数时遇到问题.inputFormToDB不是一个函数
- 在Javascript中读取json文件时遇到麻烦
- 使用FileReader api上传二进制文件时遇到问题
- 当<脚本>标签在html文件中遇到
- 从外部JS文件调用函数进行onsubmit时遇到问题
- 使用 AJAX 和 Jquery 将模态表单内容发送到 PHP 文件时遇到问题
- 在通过XML文件填充数据库和访问数据时遇到问题
- 从css文件检索高度属性时遇到问题