10

I'm trying to get the video to exit fullscreen at the end of the video but it won't. I searched and found ways to do this but for the life of me I can't get it to work. I'm testing in the latest version of Chrome (15) and iOS 5 on the iPad2. Here's the code I'm using:

<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
$(document).ready(function(){
  $("#myVideoTag").on('ended', function(){
    webkitExitFullScreen();
  });
});

</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>854x480</title>
</head>
<body>
<video  width="854" height="480"
        src="video/854x480-Template_1.mp4"
        poster="images/poster.jpg"
        id="myVideoTag"
        type="video/mp4"
        preload="auto"
        autobuffer
        controls>
  <p>Requires HTML5 capable browser.</p>
</video>

</body>
</html>

Any help will be appreciated.

fregante
  • 29,050
  • 14
  • 119
  • 159
ShockTower
  • 143
  • 1
  • 1
  • 8

3 Answers3

16

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

posit labs
  • 8,951
  • 4
  • 36
  • 66
cbaigorri
  • 2,467
  • 25
  • 26
  • Thanks cbaigorri. That was it! Thanks for the help. – ShockTower Nov 18 '11 at 23:08
  • 5
    Beware! The function name is `webkitExitFullscreen` *not* `webkitExitFullScreen` (note the lowercase "s") – Svilen Ivanov Oct 22 '14 at 21:28
  • 4
    **No one** should be up-voting a `webkit` only answer. Update your answer to include support for *all* browsers. Developers who only support one rendering engine are *NOT* developers at all! – John Feb 14 '17 at 14:39
  • Sometimes you just need a webkit answer because the other browsers are easier to work with. – Mark Rogers Oct 18 '21 at 17:07
10

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
elzaer
  • 729
  • 7
  • 25
2

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>