简体   繁体   English

视频静音/取消静音按钮 javaScript

[英]Video mute/unmute button javaScript

JavaScript beginner here! JavaScript初学者在这里!

I am trying to make a video player in javaScript for a school project but I am having trouble with my mute button.我正在尝试用 javaScript 为学校项目制作视频播放器,但我的静音按钮有问题。

I want the button to mute the video when clicked and unmute if the button is pressed again.我希望该按钮在单击时使视频静音,如果再次按下该按钮则取消静音。 So far I have only been able to mute the video and keep it muted.到目前为止,我只能将视频静音并保持静音。

Here is my current mute button.这是我当前的静音按钮。

var button = document.getElementById('mute');
    button.onclick = function (){
        video.muted = true;
    };

I tried an if else statement but it was unsuccessful我尝试了 if else 语句,但没有成功

var button = document.getElementById('mute');
    button.onclick = function (){

    if (video.muted = false) {    
           video.muted = true;
    }

    else {
        video.muted = false;
    }

    };

Thanks for your help.谢谢你的帮助。

if (video.muted = false) {    
       video.muted = true;
}

This should be这应该是

if (video.muted === false) {    
       video.muted = true;
}

Or else the else statement will never run, since you are setting video.muted to false every time in the if statement itself.否则 else 语句将永远不会运行,因为您每次都在 if 语句本身中将 video.muted 设置为 false。

输入按位异或 (^)

video.muted ^= 1

The reason is because instead of you are actually checking whether muted attribute is false or true, you are assigning false to the muted attribute.原因是因为您实际上不是在检查 muted 属性是 false 还是 true,而是将 false 分配给 muted 属性。

So you should write as follow:所以你应该写如下:

if (video.muted === false) {    
    video.muted = true;
}
else {
    // your code
};

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

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