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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19248748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:53:09  来源:igfitidea点击:

How to mute/unmute sound of an audio using javascript

javascriptjqueryaudio

提问by beginner

I have created a fiddle of my function here( 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,1should be mute and vice-versa.

我在这里创建了一个我的函数的小提琴(http://jsfiddle.net/rhy5K/10/)。现在我想为用户提供声音“静音/取消静音”选项。例如,如果我点击“一个链接,那么播放的声音Get ready,5,4,3,2,1应该是静音的,反之亦然。

I am thinking like to call a to toggle_soundfunction on click, but i am confuse like what should i write inside the function to mute the sound.

我想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 小提琴演示可能会在加载时播放声音

回答by thiyaram

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;

$('#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;

});

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;

});

回答by Raphael Wilker

I think you can use:

我认为你可以使用:

document.getElementById(id).volume = 1;

or

或者

document.getElementById(id).volume = 0;