Javascript 如何使 html5 视频播放器静音

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

How to mute an html5 video player

javascriptjqueryhtmlvideomute

提问by Cody

I've found how to pause and play the video using jquery

我找到了如何使用 jquery 暂停和播放视频

$("video").get(0).play();
$("video").get(0).pause();

But I can't find the mute button, if there isn't a jquery solution, I'm fine with just an onclick js solution. I need it asap.

Also is there a way to fix the mute delay? I want it to mute/unmute the sound as soon as the button is clicked.

但是我找不到静音按钮,如果没有 jquery 解决方案,我只需要一个 onclick js 解决方案就可以了。我需要尽快。

还有没有办法修复静音延迟?我希望它在单击按钮后立即静音/取消静音。

回答by Naftali aka Neal

$("video").prop('muted', true); //mute

AND

$("video").prop('muted', false); //unmute

See all events here

在此处查看所有活动

(side note: use attrif in jQuery < 1.6)

(旁注:attr如果在 jQuery < 1.6 中使用)

回答by delroh

If you don't want to jQuery, here's the vanilla JavaScript:

如果你不想使用 jQuery,这里是 vanilla JavaScript:

///Mute
var video = document.getElementById("your-video-id");
video.muted= true;

//Unmute
var video = document.getElementById("your-video-id");
video.muted= false;

It will work for audio too, just put the element's id and it will work (and change the var name if you want, to 'media' or something suited for both audio/video as you like).

它也适用于音频,只需输入元素的 id 即可使用(如果需要,还可以更改 var 名称,更改为“媒体”或适合音频/视频的名称)。

回答by kinakuta

Are you using the default controls boolean attribute on the video tag? If so, I believe all the supporting browsers have mute buttons. If you need to wire it up, set .muted to true on the element in javascript (use .prop for jquery because it's an IDL attribute.) The speaker icon on the volume control is the mute button on chrome,ff, safari, and opera for example

您是否在视频标签上使用默认控件布尔属性?如果是这样,我相信所有支持的浏览器都有静音按钮。如果您需要将其连接起来,请在 javascript 中的元素上将 .muted 设置为 true(将 .prop 用于 jquery,因为它是 IDL 属性。)音量控制上的扬声器图标是 chrome、ff、safari 和例如歌剧