gpt4 book ai didi

javascript - 在 javascript 数组中播放音频文件时遇到问题

转载 作者:行者123 更新时间:2023-12-02 19:34:53 25 4
gpt4 key购买 nike

我正在尝试创建一个按钮,单击该按钮时会播放音频文件,然后再次单击该按钮时会播放数组中的下一个文件。我可以播放它,但是当我第二次单击它时,它会同时播放第一个文件和第二个文件。当我第三次按下它时,它会播放第一个、第二个和第三个。似乎我需要重置某些内容才能清除页面的前两个轨道。这是我所拥有的:

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

关于javascript - 在 javascript 数组中播放音频文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com