Javascript 使用自定义按钮静音 HTML5 音频元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6442427/
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-08-23 21:44:56  来源:igfitidea点击:

Muting a HTML5 audio element using a custom button

javascripthtmlhtml5-audio

提问by Chuck Le Butt

What's the best way to mute a HTML5 audio element? I don't want the browser's custom controls, so I've been looking at the javascript instead. Unfortunately it just doesn't seem to work for me :( I'm undoubtedly doing something very wrong.

使 HTML5 音频元素静音的最佳方法是什么?我不想要浏览器的自定义控件,所以我一直在查看 javascript。不幸的是,它似乎对我不起作用:(我无疑做错了什么。

    <audio id="background_audio" autoplay="autoplay">
      <source src="static/audio/clip.ogg" type="audio/ogg" />
      <source src="static/audio/clip.mp3" type="audio/mpeg" />
    </audio> 

<a href="#" onclick="document._video.muted=true; return false">mute sound</a>

Still getting my head around this. Thanks!

仍在解决这个问题。谢谢!

回答by Matt Ball

This should work:

应该工作

document.getElementById('background_audio').muted = true;

Demo: http://jsfiddle.net/mattball/sVwXH/(no jQuery)

演示:http: //jsfiddle.net/mattball/sVwXH/(无 jQuery)