我試圖讓這4個按鈕添加到代碼中以稍後提示第5個按鈕,但是您可以忽略這一個atm。我想要的是,當我點擊一個按鈕時會播放聲音,但如果我點擊不同的聲音,它會暫停當前的聲音並播放新的聲音。我似乎並沒有得到它的工作。播放和暫停以播放聲音
CODE:
<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>