如何组合现有的全屏模式和视频暂停?

时间:2014-07-01 15:29:02

标签: javascript html5 video html5-video

我设法在一个按钮中设置了两个选项:点击时,视频开始播放并同时进入全屏。

这是html:

<video id="video1" class="video-small">
<source src="video/Marinela+Pinguinos-HD.mp4" type="video/mp4" class="video-file">
<source src="video/Marinela_Pinguinos-HD.webm" type="video/webm" class="video-file">
</video>

<button id="play" class="full-play-button" onclick="vidplay(); goFullscreen('video1')">Play fullscreen</button> 

JAVASCRIPT:

function vidplay() {
   var video = document.getElementById("video1");
   var button = document.getElementsByClassName("full-play-button");
   if (video.paused) {
      video.play();
      button.textContent = "||";
   } else {
      video.pause();
      button.textContent = ">";
   }
}

function goFullscreen(id) {
// Get the element that we want to take into fullscreen mode
var element = document.getElementById(id);

// These function will not exist in the browsers that don't support fullscreen mode yet, 
// so we'll have to check to see if they're available before calling them.

if (element.mozRequestFullScreen) {
  // This is how to go into fullscren mode in Firefox
  // Note the "moz" prefix, which is short for Mozilla.
  element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
  // This is how to go into fullscreen mode in Chrome and Safari
  // Both of those browsers are based on the Webkit project, hence the same prefix.
  element.webkitRequestFullScreen();
}
}

到目前为止事情进展顺利。进入全屏时,底部有一个类似默认播放器的东西,一个按钮可以退出全屏。

我想要实现的是能够在点击该按钮时暂停视频,但我不知道如何。

我能想到的是某种功能可以检测我们是否全屏,如果我们不是,它会暂停/停止(不确定我还不喜欢)视频。

这就是我想到的,但我真的是JS的新手而且它不起作用:

 function exitPause() {

      var video = document.getElementById("video1");

    if (document.exitFullscreen) {
        video.pause();
    }
    else if (document.mozCancelFullScreen) {
        video.pause();
    }
    else if (document.webkitExitFullscreen) {
        video.pause();
    }
    else if (element.msExitFullscreen) {
        video.pause();
    }
}

我做错了什么?我怎样才能实现呢?

2 个答案:

答案 0 :(得分:3)

使用fullscreenchange事件处理程序:

video.addEventListener(
  'fullscreenchange',
  function(event) {
    if (!document.fullscreenElement) {
      video.pause();
    }
  },
  false
);

注意:关注供应商前缀。

答案 1 :(得分:0)

感谢Igor Gilyazov,我设法走得更远了。这就是代码现在的样子:

 function vidplay() {
   var video = document.getElementById("video1");
   var button = document.getElementsByClassName("full-play-button");


   video.addEventListener(
   'webkitfullscreenchange',
    function(event) {

  if (!document.fullscreenElement && video.paused)  {
  video.play();

} 

else {
    video.pause();
    }
},
false
);


}

全屏:

function goFullscreen(id) {
// Get the element that we want to take into fullscreen mode
var element = document.getElementById(id);

// These function will not exist in the browsers that don't support fullscreen mode yet, 
// so we'll have to check to see if they're available before calling them.

if (element.mozRequestFullScreen) {
  // This is how to go into fullscren mode in Firefox
  // Note the "moz" prefix, which is short for Mozilla.
  element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
  // This is how to go into fullscreen mode in Chrome and Safari
  // Both of those browsers are based on the Webkit project, hence the same prefix.
  element.webkitRequestFullScreen();
}
// Hooray, now we're in fullscreen mode!
}

现在发生的事情是,当我第一次点击按钮时,它会全屏播放并播放视频。当回到小屏幕时,它会暂停。

这很棒。

不幸的是,它只是每隔一次发生一次(下次无论是全长还是小时都会暂停,然后再次正确,然后错误等等)。

我知道我很接近,但还没有完全正常工作,有任何修改的想法吗?