Exiting Fullscreen With The HTML5 Video Tag

柔情痞子 提交于 2019-11-30 04:14:01
cbaigorri

webkitExitFullScreen is a method of the video element, so it has to be called this way:

videoElement.webkitExitFullscreen();
//or
$("#myVideoTag")[0].webkitExitFullscreen();
//or, without needing jQuery
document.getElementsById('myVideoTag').webkitExitFullscreen();

Since it's inside an event handler, this will be the video that ended, so:

$("#myVideoTag").on('ended', function(){
  this.webkitExitFullscreen();
});

It gets a bit more complicated because webkitExitFullscreen only works in webkit-based browsers (Safari, Chrome, Opera), so you can learn more about its correct usage on MDN

elzaer

I know this is already answered, but here is the little code snippet I ended up using for all browsers to close fullscreen video after it ends.

Works on Chrome, IE11, Firefox so far:

$("#myVideoTag").on('ended', function(){
    if (document.exitFullscreen) {
        document.exitFullscreen(); // Standard
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen(); // Blink
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen(); // Gecko
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen(); // Old IE
    }
});

You can also find the current full screen element (if any) like:

  var fullscreenElement = document.fullscreenElement || 
   document.mozFullScreenElement || document.webkitFullscreenElement;

Source: https://www.sitepoint.com/use-html5-full-screen-api/

Just thought I would add the answer as this was the first question I came across when looking for a solution to this.

Thank you cbaigorri, it did work wonderfully to use .webkitExitFullscreen();

I used the following to exit fullscreen when the video is done playing:

<script type="text/javascript">
function CloseVideo() {
    document.getElementsByTagName('video')[0].webkitExitFullscreen();
}
</script>

<video controls onended=CloseVideo() >
    <source src="video.mp4" type="video/mp4">
</video>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!