html5 video mute按钮,How to mute an html5 video player

这篇博客介绍了如何使用jQuery和原生JavaScript来控制HTML5视频的播放、暂停以及静音功能。通过`$(video).get(0).play()`和`$(video).get(0).pause()`可以实现播放和暂停,而`$(video).prop('muted', true)`和`$(video).prop('muted', false)`则用于静音和取消静音。如果遇到静音延迟问题,可以通过直接操作视频元素的`muted`属性即时切换声音状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I've found how to pause and play the video using 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.

回答1:

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

AND

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

See all events here

(side note: use attr if in jQuery < 1.6)

回答2:

If you don't want to jQuery, here's the 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).

回答3:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值