gpt4 book ai didi

javascript - 播放和暂停播放声音

转载 作者:行者123 更新时间:2023-12-03 01:56:38 25 4
gpt4 key购买 nike

我试图将这4个按钮添加到代码中,以在以后暗示第5个按钮,但是您可以忽略该一个atm。我想要的是,当我单击一个按钮时它会播放声音,但是如果我单击另一按钮,它会暂停当前声音并播放新的声音。我似乎没有得到它的工作。

码:

<audio id="sound1" src="monkey.mp3"></audio>
<audio id="sound2" src="sounds/sound2.mp3"></audio>
<audio id="sound3" src=".mp3"></audio>
<audio id="sound4" src=".mp3"></audio>
<audio id="sound5" src=".mp3"></audio>

<button onclick="playSound1()">Sound 1</button><br />
<button onclick="playSound2()">Sound 2</button><br />
<button onclick="playSound3()">Sound 3</button><br />
<button onclick="playSound4()">Sound 4</button><br />
<button onclick="playSound5()">Sound 5</button><br />

<script type="text/javascript">
var audio1 = document.getElementById('sound1');
var audio2 = document.getElementById('sound2');
var audio3 = document.getElementById('sound3');
var audio4 = document.getElementById('sound4');
function playSound1(){
if ((audio1.paused !== true) && (audio.3paused !== true) && (audio4.paused !==){
audio1.pause();
audio3.pause();
audio4.pause();
audio2.play();
}
else{
audio2.play();
}
}
function playSound2(){
if (audio2.paused !== true){
audio2.pause();
audio3.pause();
audio4.pause();
audio1.play();
}
else{
audio1.play();
}
}
function playSound3(){
if (audio2.paused !== true){
audio2.pause();
audio1.pause();
audio3.pause();
audio4.play();
}
else{
audio4.play();
}
}
function playSound4(){
if (audio2.paused !== true){
audio2.pause();
audio4.pause();
audio1.pause();
audio3.play();
}
else{
audio3.play();
}
}

</script>

最佳答案

var audio1 = document.getElementById('sound1');
var audio2 = document.getElementById('sound2');
var audio3 = document.getElementById('sound3');
var audio4 = document.getElementById('sound4');

var current = "";

var playSound = function(soundNumber){
switch(soundNumber)
{
case 1:
audio1.play();
audio2.pause();
audio3.pause();
audio4.pause();
break;
case 2:
audio1.pause();
audio2.play();
audio3.pause();
audio4.pause();
break;
case 3:
audio1.pause();
audio2.pause();
audio3.play();
audio4.pause();
break;
case 4:
audio1.pause();
audio2.pause();
audio3.pause();
audio4.play();
break;
default:
var audio = document.getElementById("sound"+soundNumber);
audio.play();
audio1.pause();
audio2.pause();
audio3.pause();
audio4.pause();
break;
}
}
<audio id="sound1" src="monkey.mp3"></audio>
<audio id="sound2" src="sounds/sound2.mp3"></audio>
<audio id="sound3" src=".mp3"></audio>
<audio id="sound4" src=".mp3"></audio>
<audio id="sound5" src=".mp3"></audio>

<button onclick="playSound(1)">Sound 1</button><br />
<button onclick="playSound(2)">Sound 2</button><br />
<button onclick="playSound(3)">Sound 3</button><br />
<button onclick="playSound(4)">Sound 4</button><br />
<button onclick="playSound(5)">Sound 5</button><br />


按钮°5应该与默认开关盒配合使用。如果必须一般性地添加按钮,则必须更改功能。

关于javascript - 播放和暂停播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918462/

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