我使用下面的代码(我在网上找到的)在我的网站上单击按钮时播放音频,现在我很好奇我可以做什么来使音频暂停和/或停止播放当使用类似的代码单击相同的按钮时?
const rollSound = new Audio("./mp3/SoItGoes.mp3");
$('#Triangle').click(e => rollSound.play());
最佳答案
您可以在按钮上使用一个类来指定播放器的状态(class = "playing"
如果正在播放,则不显示任何内容,如果暂停则不显示任何内容),并在播放器播放时检查它单击按钮:
HTML:
<button id="Triangle">Play/Pause</button>
JavaScript:
$('#Triangle').click(function(e) {
if ($(this).hasClass('playing')) {
rollSound.pause();
$(this).removeClass('playing');
} else {
rollSound.play();
$(this).addClass('playing');
}
});
关于javascript - 如何在点击时暂停音乐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61845479/