简体   繁体   English

如何使用 jQuery 使 html5 视频播放器静音

[英]How to mute an html5 video player using jQuery

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.但是我找不到静音按钮,如果没有 jquery 解决方案,我只需要一个 onclick js 解决方案就可以了。 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.我希望它在单击按钮后立即静音/取消静音。

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

AND

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

See all events here在此处查看所有活动

(side note: use attr if in jQuery < 1.6) (旁注:如果在 jQuery < 1.6 中使用attr

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 名称,更改为“媒体”或适合音频/视频的名称)。

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 和例如歌剧

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

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