Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Youtube player doesn't exit from full screen

Well, I think this is a major Youtube bug but I don't find any report about it.

I have a web app which is displayed in full screen browser using the JavaScript Fullscreen API.

In the web app there is an embedded Youtube player. When you open the Youtube player in fullscreen, then clicks the Youtube's fullscreen button again to exit the player's fullscreen, it doesn't respond!

I am sure it is related to the fact that the browser is already in full screen mode so there is some kind of conflict.

I have created a simplified example which can be viewed here: http://run.plnkr.co/CjrrBGBvrSspfa92/

  1. Click the "GO FULLSCREEN" button.
  2. Play the video and click the fullscreen button. The video will go fullscreen.
  3. Click the fullscreen button again. It won't exit.

EDIT: The code for the html file above is here:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<iframe width="560" height="315" src="https://www.youtube.com/embed/b-6B2zyoFsI" frameborder="0" allowfullscreen></iframe>
<button id="btn">GO FULLSCREEN</button>

<script type="text/javascript">
    document.getElementById("btn").addEventListener("click", function() {
        var elem = document.documentElement;
        if (elem.requestFullscreen) {
            elem.requestFullscreen();
        } else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        } else if (elem.mozRequestFullScreen) {
            elem.mozRequestFullScreen();
        } else if (elem.webkitRequestFullscreen) {
            elem.webkitRequestFullscreen();
        }
    });
</script>
</body>
</html>
like image 744
Light Avatar asked Jun 21 '15 10:06

Light


2 Answers

The thing is I did some searching and it seams Youtube doesn't know if the video is in fullscreen or not when using the JavaScript Fullscreen API nor does Google provide an API call to fo in or out of fullscreen. So, When you click the button and it goes in fullscreen, you'll see the player's fullscreen button not pressed. So, in order to get back to the window view, the user has two options: 1) click on the button 2 times (the first time, the player tries to go in fullscreen and the button changes state, the second time, the player goes in window mode) - this is the solution 2) click Esc on the keyboard.

I checked with the HTML5 player.

Furthermore, I tried injecting a button inside YouTube's iframe so I can select it in order to exit fullscreen, but it didn't work... would have been silly to actually.

This should work:

<div id="videoplayer"></div>
<p><button id="btn">GO FULLSCREEN</button></p>

<script type="text/javascript">
var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('videoplayer', {
      height: '380',
      width: '500',
      videoId: 'h7ArUgxtlJs',
      fs: 1
  });
}

document.getElementById("btn").addEventListener("click", function() {
  var elem = document.getElementById('videoplayer');
  var requestFullScreen = elem.requestFullscreen || elem.msRequestFullscreen || elem.mozRequestFullScreen || elem.webkitRequestFullscreen;
  if (requestFullScreen) {
    requestFullScreen.bind(elem)();
  }
});
</script>

You can use the classic embed, I belive.

Working demo

like image 145
mihaidp Avatar answered Dec 01 '22 22:12

mihaidp


This may help: "Exiting Fullscreen Mode":

// Whack fullscreen
function exitFullscreen() {
  if(document.exitFullscreen) {
    document.exitFullscreen();
  } else if(document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if(document.webkitExitFullscreen) {
    document.webkitExitFullscreen();
  }
}

// Cancel fullscreen for browsers that support it!
exitFullscreen();

Source.


Side note: Youtube's API is still pretty scarce in terms of what you can customize even in 2015 (due to how it relies on so much iFrame, and they don't want you to reskin their player). You'll likely get to a point where your using a bunch of funky JavaScript hacks to get what you want, which can get messy and unstable. It would be better practice to utilize one of many customizable video players where you can have more control with JS; like JW player, Video.js, Flow player, popcorn.js etc.

like image 33
fred randall Avatar answered Dec 01 '22 21:12

fred randall