embed youtube html5 player shows no fullscreen button

雨燕双飞 提交于 2019-12-12 15:04:21

问题


I include the YouTube player as follows in my php file but the player does not show the fullscreen button. Switching to the flash player works (whether through changing the url from /embed to /v or by disabling &html5=1). What am I doing wrong?

An example is available here: http://jonnyrimkus.square7.ch/stuff/youtube_html5_fullscreen.php

<script>

var tag = document.createElement(\'script\');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName(\'script\')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


function onYouTubeIframeAPIReady() {
  player = new YT.Player(\'player\', {
    playerVars: {
      \'allowfullscreen\': \'true\',                
      \'allowscriptaccess\': \'always\'
    },
    events: {
      \'onReady\': onYouTubePlayerReady,
      \'onStateChange\': playerStateChange,
      \'onError\': playerStateError
    }
  });
}
</script>   
<iframe id="player" width="425" height="356" border="0" frameborder="0" src="http://www.youtube.com/embed/36XdO9Iv9ew?enablejsapi=1&playerapiid=lastfmplayer&autoplay=1&html5=1&fs=1&origin=http://jonnyrimkus.square7.ch"></iframe>

回答1:


The way you are using the iframe api now does nothing, the api is made to bind on an empty element, like <div id="player"></div>, the id is the first argument in the new YT.Player function.

In order to load a youtube video with the iframe api you need this in the body:

<div id="player"></div>

<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: 480,
        width: 640,
        videoId: "36XdO9Iv9ew",
    });
}
</script>

There is no need to explicitely specify you want to enable fullscreen when using the iframe api.

You can also just use the iframe without the api, you'll need to specify you want fullscreen when you use it.

<iframe width="640" height="480" frameborder="0" id="player" allowfullscreen="1" title="YouTube video player" src="http://www.youtube.com/embed/36XdO9Iv9ew?enablejsapi=1"></iframe>

Just using the iframe tag is a bit faster, but if you want to use the extra features of the iframe api you have no choice.

A page with examples (also check the source): http://qnet.co/yt

You can also implement the fullscreen feature yourself (not needed for Youtube, but still cool):

var goFullscreen = function(id) {
  var el = document.getElementById(id);
  if (el.requestFullScreen) {
    el.requestFullScreen();
  } else if (el.mozRequestFullScreen) {
    el.mozRequestFullScreen();
  } else if (el.webkitRequestFullScreen) {
    el.webkitRequestFullScreen();
  }
}

var leaveFullscreen = function() {
  if (document.cancelFullScreen) {
    document.cancelFullScreen();
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if (document.webkitCancelFullScreen) {
    document.webkitCancelFullScreen();
  }
}

and to make the Youtube player go fullscreen with: goFullscreen('player'), and leave fullscreen with: leaveFullscreen()

The different versions of requestFullscreen and cancelFullscreen are for different browsers, because the standard is not yet completely finished

More info on Javascript Fullscreen: http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/ (relative old document, but still valid)

off-topic: It is useless to echo such a string with php, you can just paste it in the body the file outside of the php tags.




回答2:


The fullscreen button will also not be visible if the Youtube player is inside another iframe that does not have allowfullscreen attribute.

Unlike what Google's documentation says(as of 11/2014), the fs attribute in querystring does not seem to influence the visibility of fullscreen. The visibility seems to be influenced by allowfullscreen attribute in iframe which youtube player puts by default during instantiation. That said, if your embed the player inside another iframe you should also mark that iframe for allowfullscreen ( or all its variants webkitallowfullscreen mozallowfullscreen)

<iframe src='' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen>
       <!-- YT player-->
</iframe>



回答3:


This is still an issue in July 2014, and you just wonder if Google will ever fix this. Actually you can force the Flash player in another way at the client end by using a UA Spoofer, and for Google Chrome browser for instance, Chrome Web Store - djflhoibgkdhkhhcedjiklpkjnoahfmg and then spoof a browser that doesn't understand HTML5.

Actually HTML5 video is still a disaster, and the grainey spikey-jaggy edges to the video and the herringbone patterning though faint is still distracting. Whereas Flash is Smooth, Flawless, Reliable, and Sharp edges with zero patterning artifacts.

HTML5 - still big thumbs down, I wouldn't inflict it on users.

Oh yes and still Fullscreen not appear in embeds like this Rick Astley - Never Gonna Give You Up @ viewpure embed http://viewpure.com/dQw4w9WgXcQ

You can use the above example to fiddle and diddle with different browser plugins.



来源:https://stackoverflow.com/questions/16597956/embed-youtube-html5-player-shows-no-fullscreen-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!