最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Embedded Youtube player doesn't exit from full screen - Stack Overflow

programmeradmin2浏览0评论

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: /

  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="" 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>

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./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>
Share Improve this question edited Apr 30, 2017 at 12:27 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Jun 21, 2015 at 10:48 LightLight 1,6773 gold badges23 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4 +25

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./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

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.

发布评论

评论列表(0)

  1. 暂无评论