gpt4 book ai didi

javascript - 播放新音频时停止播放音频

转载 作者:行者123 更新时间:2023-12-03 02:00:10 25 4
gpt4 key购买 nike

如果按下按钮,它将播放新声音并停止当前正在播放的声音。它还应使其在播放声音时启用“暂停”类。但是,如果没有播放与按钮关联的声音,则该类是正常的,即“播放”。

//this is the buttons that can be pressed to play sound
<span class="play" sound="sound1.mp3">Play</span>
<span class="play" sound="sound2.mp3">Play</span>
<span class="play" sound="sound3.mp3">Play</span>

<script>
// trying to make it so that the var audio selects sound based on what button was pressed
var audio = document.createElement('sound');
//when a button is pressed plays sound
$(".play").on('click', function(){

if (audio.paused) {
audio.play();
}
else {
audio.pause();
}
$( this ).toggleClass( "pause" ); // switch to some new css for a pause button
});

// make it so if audio is already playing and another sound is pressed, the audio that is playing is stopped and newly pressed sound plays.
if any other audio is playing{
then that audio.stop and this.audio. that was clicked. play
}
// make it so the CSS of the already playing button is switched back and the css of the new button is turned to the pause class.
$( that ).toggleClass( "play" );
$( this ).toggleClass( "pause" );
</script>

最佳答案

我只是将音频元素嵌入跨度中,如下所示:

<span class="play"><audio src="sound1.mp3">Play</audio></span>
<span class="play"><audio src="sound2.mp3">Play</audio></span>
<span class="play"><audio src="sound3.mp3">Play</audio></span>

然后做一些jQuery魔术
$(".play").click(function () {
$(".play").removeClass('pause').addClass('play').pause();
$(this).addClass('pause').removeClass('play').play();
});

关于javascript - 播放新音频时停止播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32303933/

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