我在这里创建了一个 fiddle 函数 (http://jsfiddle.net/rhy5K/10/)。现在我想为用户提供声音“静音/取消”选项。例如,如果我点击“一个链接,然后播放的声音像 Get ready,5,4,3,2,1
应该静音,反之亦然。
我想在点击时调用 toggle_sound
函数,但我很困惑我应该在函数中写些什么来静音。
请为我的问题提供逻辑或解决方案。
使用代码示例进行说明:
我想静音/取消静音下面正在播放的声音
var playGetReady = function (done) {
var ids = ['audiosource', 'a_5', 'a_4', 'a_3', 'a_2', 'a_1'],
playNext = function () {
var id = ids.shift();
document.getElementById(id).play();
if (ids.length) {
setTimeout(playNext, 1000);
} else {
done();
}
};
playNext();
};
警告:此 JS fiddle 演示可能会在加载时播放声音
最佳答案
尝试
HTML5 Video_tag for display video with audio
或者 下面这个例子连同带有 jquery 的 html5
window.my_mute = false;
$('#my_mute_button').bind('click', function(){
$('audio,video').each(function(){ if (!my_mute ) { if( !$(this).paused ) { $(this).data('muted',true); //Store elements muted by the button. $(this).pause(); // or .muted=true to keep playing muted } } else { if( $(this).data('muted') ) { $(this).data('muted',false); $(this).play(); // or .muted=false } } }); my_mute = !my_mute;
});
关于javascript - 如何使用 javascript 使音频静音/取消静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19248748/