VideoJS播放器自定义静音/取消静音切换

问题描述

我需要为VideoJs播放器添加一个自定义的静音/取消静音按钮(当我单击该按钮时,如果该按钮被静音,则应取消静音,而如果将其取消静音则应使其静音)。

我尝试使用JavaScript通过以下方式执行此操作,但是它不起作用。你能帮我解决这个问题吗?

const muteButton = document.querySelector(".mute-btn11");

muteButton.addEventListener("click",function() {
  var video = videojs("myVideo");
  const booleanValue = video.muted.valueOf();
  console.log(booleanValue);

  if (booleanValue == true) {
    video.muted(false);
  } else {
    video.muted(true);
  }
});
<video controls autoplay playsinline id="myVideo" class="video-js vjs-16-9 vjs-big-play-centered"></video>
<button class="mute-btn11"> Mute </button>

解决方法

静音是 VideJS Player 对象的一个​​功能。

您可以使用此方法获取视频的 Player 对象。

var vp = videojs('myVideo').player();

然后,您可以调用 muted() 函数来获取布尔值。

var muted = vp.muted(); 

使用真假参数设置使用静音方法

var muted = vp.muted( true ); 

var muted = vp.muted( false );