如何静音html5视频播放器

时间:2021-07-14 15:26:09

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解决方案就可以了。我尽快需要它。还有办法解决静音延迟吗?我希望它在单击按钮后立即静音/取消静音。

3 个解决方案

#1


93  

$("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)

#2


45  

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名称,更改为'media'或适合音频/视频的内容)。

#3


0  

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上的静音按钮,歌剧,例如

#1


93  

$("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)

#2


45  

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名称,更改为'media'或适合音频/视频的内容)。

#3


0  

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上的静音按钮,歌剧,例如