简体   繁体   English

如何使用 javascript 静音/取消静音音频

[英]How to mute/unmute sound of an audio using javascript

I have created a fiddle of my function here( http://jsfiddle.net/rhy5K/10/ ) .我在这里创建了一个我的函数的小提琴( http://jsfiddle.net/rhy5K/10/ )。 Now i want to provide the Sound "Mute/unpute" option for users.现在我想为用户提供声音“静音/取消静音”选项。 Example if i click on "a link then sound playing like Get ready,5,4,3,2,1 should be mute and vice-versa.例如,如果我点击“一个链接,然后听起来像Get ready,5,4,3,2,1应该静音,反之亦然。

I am thinking like to call a to toggle_sound function on click, but i am confuse like what should i write inside the function to mute the sound.我想在点击时调用 to toggle_sound函数,但我很困惑,我应该在函数内部写什么来静音。

Please provide me a logic, or a solution for my problem.请为我提供逻辑或解决我的问题的方法。

Explanation using code example:使用代码示例说明:

I want to MUTE/UnMUTE this below sound playing我想静音/取消静音下面的声音播放

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();
};

Warning: This JS fiddle demo may play sound on load警告:此 JS 小提琴演示可能会在加载时播放声音

Try尝试

HTML5 Video_tag for display video with audio HTML5 Video_tag 用于显示带音频的视频

or this below example along with html5 with jquery或下面的示例以及带有 jquery 的 html5

window.my_mute = false; window.my_mute = false;

$('#my_mute_button').bind('click', function(){ $('#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;

}); });

I think you can use:我认为你可以使用:

document.getElementById(id).volume = 1;

or要么

document.getElementById(id).volume = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM