简体   繁体   English

HTML5视频播放器的静音和音量按钮

[英]Mute and Volume Button for HTML5 Video Player

I'm trying to enter in some JavaScript code that'll cause the volumeslider to go all the down when the mute button is clicked. 我正在尝试输入一些JavaScript代码,当单击“静音”按钮时,这将导致volumeslider下降。

The functions I have for my mute button and volume slider is here 我的静音按钮和音量滑块的功能在这里

    function vidmute () { 
     if(vid.muted){ 
        vid.muted = false; 
        mutebtn.innerHTML = "Mute"; 

    } else { 
         vid.muted= true; 
         mutebtn.innerHTML = "Unmute";   
     } 
}

       function setVolume () { 
          vid.volume = volumeslider.value / 100; 
}

I've tried entering in vid.volume = volumeslider.value / 100 for the if statement for vidmute and vid.volume = volumeslider.value / 0 for the else statement for vidmute and it doesn't seem to work. 我尝试为vidmuteif语句输入vid.volume = volumeslider.value / 100 ,为vidmuteelse语句输入vidmute vid.volume = volumeslider.value / 0 ,这似乎不起作用。 I also tried these statements vid.mutebtn = volumeslider.value / 100 and vice versa and still doesn't work. 我也尝试了这些语句vid.mutebtn = volumeslider.value / 100 ,反之亦然,仍然无法正常工作。 Can anyone help me with this? 谁能帮我这个?

Use vid.volume = 0 . 使用vid.volume = 0

This is what vid.volume = volumeslider.value / 100; 这就是vid.volume = volumeslider.value / 100; is doing: 是在做:

  1. get volumeslider 's value (between 0 and 100) 获取volumeslider的值(0到100之间)
  2. divide it by 100 to get a result between 0.0 and 1.0 用100除以得到0.0到1.0之间的结果
  3. assign the value to vid.volume 将值分配给vid.volume

By changing vid.volume = volumeslider.value / 100 to vid.volume = volumeslider.value / 0 you were actually increasing* the value (and obviously passing a value larger than the maximum accepted doesn't mute the volume) 通过将vid.volume = volumeslider.value / 100更改为vid.volume = volumeslider.value / 0您实际上是在增加*值(显然,传递的值大于可接受的最大值不会使音量静音

*Theoretically; *理论上; decreasing the divisor would produce a larger result. 减小除数将产生更大的结果。 In any programming language this is not allowed and would throw an error. 在任何编程语言中,这都是不允许的,并且会引发错误。

I'm assuming you have a separate volumeslider element in your HTML and you want it to be set to 0 if you click a mute button. 我假设您的HTML中有一个单独的volumeslider元素,如果您单击一个静音按钮,则希望将其设置为0。

Try this code: 试试这个代码:

function vidmute () { 
  if(vid.muted){ 
    vid.muted = false; 
    mutebtn.innerHTML = "Mute";
    vidslider.value = 0;
  } else { 
    vid.muted = true; 
    mutebtn.innerHTML = "Unmute";   
    vidslider.value = vid.volume * 100;
  } 
}

This works as follows: vid.volume is the volume of your video. 其工作方式如下:vid.volume是视频的音量。 It's values are between 0 and 1. 它的值在0到1之间。

vidslider.value is the position of your slider, it's values are between 0 and 100. vidslider.value是滑块的位置,其值在0到100之间。

You can assign a value to a variable if you put the variable to the left of the "=" like this: 如果将变量放在“ =”的左侧,则可以为变量分配值,如下所示:

a = 5;

Now the value of "a" is set to 5. 现在,“ a”的值设置为5。

You want to set the value of your slider to 0. So you put your slider to the left side of the "=": 您要将滑块的值设置为0。因此,将滑块放在“ =”的左侧:

vidslider.value = 0;

Now your slider is set to the leftmost position, if the video was muted. 现在,如果视频已静音,则将滑块设置在最左侧。

If you click "unmute", you want the slider to be set to the original position. 如果单击“取消静音”,则希望将滑块设置为原始位置。 Therefore you first need to read the volume of the video, and then set it as the value of the slider. 因此,您首先需要阅读视频的音量,然后将其设置为滑块的值。

vidslider.value = vid.volume;

Now remember that the vidslider value could be 0-100, whereas the vid volume has a range 0-1. 现在请记住,vidslider值可以是0-100,而vid音量的范围是0-1。 So you have to multiply the volume with 100 to get it to the correct range 因此,您必须将音量乘以100才能达到正确的范围

vidslider.value = vid.volume * 100;

Have fun and please try to ask questions with the whole problem explained in a short example, so we know exactly what you need to know. 祝您玩得开心,请尝试用简短的示例说明整个问题,以便我们确切地了解您需要知道的内容。

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

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