使用iframe API在Android手机上播放Youtube html5视频

时间:2014-06-21 21:00:11

标签: android html5 youtube youtube-iframe-api

我使用以下代码播放HTML5视频

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>
    <script type="text/javascript" id="youtubeplayer">
    </script>
    <script>
    document.getElementById('youtubeplayer').style.width=window.innerWidth;
    document.getElementById('youtubeplayer').style.height=window.innerHeight;
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

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

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {

          //videoId: localStorage.getItem('videoid'),
          videoId : "8UVNT4wvIGY",
          width : Math.abs(window.innerWidth*0.99),
          height : Math.abs(window.innerHeight*0.99),
          allowFullScreen : true,
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING && !done) {
          //setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>

基本上我将它嵌入HTML5页面。 它在桌面上运行良好,但在Android设备(谷歌Nexus 10)上,它只显示视频的标题,视频无法加载。

1 个答案:

答案 0 :(得分:0)

Fitvids插件对我来说效果非常好,并且在我使用的引导框架中运行良好。 从他们的github页面:

<script src="path/to/jquery.min.js"></script>
<script src="path/to/jquery.fitvids.js"></script>
<script>
  $(document).ready(function(){
     // Target your .container, .wrapper, .post, etc.
     $("#thing-with-videos").fitVids();
  });
</script>
忙完了!