【问题标题】:Playing Youtube html5 videos on Android Phones using iframe API使用 iframe API 在 Android 手机上播放 Youtube html5 视频
【发布时间】:2014-06-21 21:00:11
【问题描述】:

我正在使用以下代码播放 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 设备 (Google Nexus 10) 上,它只显示视频的标题并且视频无法加载。

【问题讨论】:

    标签: android html youtube youtube-iframe-api


    【解决方案1】:

    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>
    

    成功了!

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 2015-02-14
      • 2017-07-09
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2014-11-21
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多