【问题标题】:How to mute/unmute sound of an audio using javascript如何使用javascript静音/取消静音音频的声音
【发布时间】:2013-10-15 10:30:23
【问题描述】:

我在这里创建了我的函数的小提琴(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 演示可能会在加载时播放声音

【问题讨论】:

标签: javascript jquery audio


【解决方案1】:

试试

HTML5 Video_tag 用于显示带音频的视频

或 下面这个例子以及带有 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;

});

【讨论】:

  • 根据我的问题,请删除您的答案,它对我没有用。它既不能解决我的问题
【解决方案2】:

我认为你可以使用:

document.getElementById(id).volume = 1;

document.getElementById(id).volume = 0;

【讨论】:

  • 但在应用此功能之前,我如何检查声音是否正在播放?你能更新我的小提琴吗(jsfiddle.net/rhy5K/10)?
  • 那些我知道的链接,但你真的想帮助我的意思是,请按照我想要的答案更新我的小提琴。您的链接不能解决我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多