我制作了这张图片,因此当您单击它时,音频开始播放。但是,因为我正在使用一首完整的歌曲,而不仅仅是一点声音,所以我还必须能够停止它。
我该如何做才能打开和关闭声音?
<audio id="1st_sound">
<source src="http://k007.kiwi6.com/hotlink/nbgq3cyywi/Damn_Good_Wife.mp3" type="audio/mpeg" />
</audio>
<img src="http://static.tumblr.com/6s0fefr/t3wne2irz/damngoodwifeplaybutton.jpg" onClick="document.getElementById('1st_sound').play(); return false;" />
最佳答案
您可以使用此功能检查音频是否正在播放:
function isPlaying(playerId) {
var player = document.getElementById(playerId);
return !player.paused && !player.ended && 0 < player.currentTime;
}
您可以在if语句中使用它。
if (isPlaying(...)) {..Pause..} else {..Play..}
要使声音静音,可以使用以下命令:Muting a HTML5 audio element using a custom button
if(document.getElementById('background_audio').muted) {
document.getElementById('background_audio').muted = true;
} else {
document.getElementById('background_audio').muted = false;
}
关于jquery - 将OnClick Stop添加到音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577658/