0

I'm running into a bug when making the YouTube player to go into fullscreen mode via JavaScript.

I have a button that when it's clicked, it triggers the YT.Player.playVideo() method, while at the same time asking the containing iframe to go into fullscreen as follows:

thisVid = document.getElementById("ytIframe");

if (thisVid.requestFullscreen) {
    thisVid.requestFullscreen();
}
else if (thisVid.msRequestFullscreen) {
    thisVid.msRequestFullscreen();
}
else if (thisVid.mozRequestFullScreen) {
    thisVid.mozRequestFullScreen();
}
else if (thisVid.webkitRequestFullScreen) {
    thisVid.webkitRequestFullScreen();
}

(This is using Stack Overflow answers here and here)

The iframe successfully takes up the screen, however, the YouTube player doesn't know it's in fullscreen mode because the fullscreen button in the bottom-right is still available: Fullscreen still available

When I click it, it stays in fullscreen, and it tells me "youtube.com is now fullscreen":Now YouTube is fullscreen

This is a problem because the user needs to click the button twice to be able to exit fullscreen mode. Is there a way to tell the youtube player to go fullscreen, instead of just telling its containing iframe to do so?

Community
  • 1
  • 1
M -
  • 26,908
  • 11
  • 49
  • 81
  • remove the control button can be a solution. – mpgn Sep 03 '14 at 08:42
  • I'd like to, but the only option is `controls: 0` and that removes volume, play/pause, and the seek bar. I would hate to leave the user without access to all those elements. – M - Sep 03 '14 at 18:00
  • you can make your custom button play/pause/volume – mpgn Sep 03 '14 at 19:57

1 Answers1

4

Because the YouTube player actually exists inside an iFrame, when you use the javascript fullscreen API you're full screening the iframe, not the player. So as far as it's concerned it is not full screen. The only way to get the player to go to full screen mode is for the user to click the button manually (this is a legacy requirement, as the iframe could still contain the Flash player, and Flash has security measures to prevent programmatic fullscreening). For now your only solution is a custom control bar (and even then you won't be full screening the player, but just its container ... but at least you'd have control over what the buttons do).

jlmcdonald
  • 13,408
  • 2
  • 54
  • 64
  • Yeah, it's a letdown that the YouTube player doesn't know it's in fullscreen mode. YouTube should enable fullscreening via their API as long as it's close to a user-generated click. I mean, if security is a concern, full-screening the iFrame without video controls has exactly the same implications. – M - Sep 04 '14 at 01:17
  • 1
    As I alluded to, if YouTube could ditch the legacy Flash player, it wouldn't be a concern, and they could expose the fullscreen control to javascript; but because there's always the chance on desktop that there will be a Flash player in the iFrame, and Flash itself has the security concerns about fullscreening (basically, the browser can't programmatically give up the screen to a plugin unless it knows the user has initiated it), this is what we have to deal with. Android and iOS YouTube player APIs, which don't support flash, have methods for setting the player to fullscreen mode. – jlmcdonald Sep 04 '14 at 01:22
  • "browser can't programmatically give up the screen to a plugin unless it knows the user has initiated it" same with HTML, it has to come from user initation (button) And a sidenote, Flash can fully interface with javascript. (not that it matters these days where flash is doomed ) – Jakob Sternberg Mar 10 '17 at 13:14
  • No, HTML5 can initiate fullscreen under certain circumstances with the Fullscreen API (https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API). There are restrictions, but it has much more flexibility as to when it can function without requiring user interaction. – jlmcdonald Mar 14 '17 at 17:11