【问题标题】:Youtube Iframe API loadVideoById() skips the videoYoutube Iframe API loadVideoById() 跳过视频
【发布时间】:2015-10-09 05:06:38
【问题描述】:

问题

我正在使用 Youtube Iframe API,并且我需要使用 API 的每个视频的 startSeconds 和 endSeconds 选项。

但问题是:loadVideoById 函数会跳过视频。 在我的示例代码中,只播放了第一个、第三个、第五个视频。

这是代码。它只是我的应用程序的简化版本。

源代码

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');
      tag.src = "http://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', {
          height: '390',
          width: '640',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      var videos = [
        {
          vid: 'nbCqWMIT-Pk',
          startSeconds: 10,
          endSeconds: 15
        },
        {
          vid: 'sAoJGNF_laA',
          startSeconds: 10,
          endSeconds: 15
        },
        {
          vid: 'kF_23_XxaHQ',
          startSeconds: 10,
          endSeconds: 15
        },
        {
          vid: 'zxYp3I0Gyrg',
          startSeconds: 10,
          endSeconds: 15
        },
        {
          vid: 'U_gA99OQwO4',
          startSeconds: 10,
          endSeconds: 15
        }
      ];
      var index = 0;
      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.cueVideoById({
          videoId: videos[index].vid,
          startSeconds: videos[index].startSeconds,
          endSeconds: videos[index].endSeconds
        });
        event.target.playVideo();
      }

      function onPlayerStateChange(event) {
        if (event.data === YT.PlayerState.ENDED) {
          console.log(index);
          if (index < videos.length - 1) {
            index++;
            event.target.loadVideoById({
              videoId: videos[index].vid,
              startSeconds: videos[index].startSeconds,
              endSeconds: videos[index].endSeconds
            });
          }
        }
      }

    </script>
  </body>
</html>

源码简单说明

视频数组被分配用于我自己的测试。 (这都是 Faker 的亮点,因为我是他的忠实粉丝 :)

我让播放器播放列表中的视频。如果播放器发出“ENDED”事件,则更改索引​​并播放下一个视频。

但是当第二个和第四个视频要播放的时候,这个视频没有播放。只有 ENDED 事件到来。

加载的视频的 endSeconds 值似乎不等于视频的总时长,它跳过了下一个视频。

我该如何解决这种情况?是 Youtube API 的问题吗?

【问题讨论】:

    标签: javascript iframe video youtube


    【解决方案1】:

    我添加了一个日志函数来查看调用了哪些状态,这似乎是 API 中的问题。当新视频加载时,状态为 -1(未开始),紧接着状态为 0(结束)。

    为防止这种情况发生,我刚刚添加了一项额外检查,以确保至少加载了一小部分视频。

    <!DOCTYPE html>
    <html>
      <body>
        <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
        <div id="player"></div>
    
        <script>
          // 2. This code loads the IFrame Player API code asynchronously.
          var tag = document.createElement('script');
          tag.src = "http://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', {
              height: '390',
              width: '640',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }
    
          var videos = [
            {
              vid: 'nbCqWMIT-Pk',
              startSeconds: 10,
              endSeconds: 15
            },
            {
              vid: 'sAoJGNF_laA',
              startSeconds: 10,
              endSeconds: 15
            },
            {
              vid: 'kF_23_XxaHQ',
              startSeconds: 10,
              endSeconds: 15
            },
            {
              vid: 'zxYp3I0Gyrg',
              startSeconds: 10,
              endSeconds: 15
            },
            {
              vid: 'U_gA99OQwO4',
              startSeconds: 10,
              endSeconds: 15
            }
          ];
          var index = 0;
          // 4. The API will call this function when the video player is ready.
          function onPlayerReady(event) {
            event.target.cueVideoById({
              videoId: videos[index].vid,
              startSeconds: videos[index].startSeconds,
              endSeconds: videos[index].endSeconds
            });
            event.target.playVideo();
          }
    
          function onPlayerStateChange(event) {
            console.log("State change: " + event.data + " for index: " + index);
    
            if (event.data === YT.PlayerState.ENDED && player.getVideoLoadedFraction() > 0) {
              console.log(index);
              if (index < videos.length - 1) {
                index++;
                event.target.loadVideoById({
                  videoId: videos[index].vid,
                  startSeconds: videos[index].startSeconds,
                  endSeconds: videos[index].endSeconds
                });
              }
            }
          }
    
        </script>
      </body>
    </html>
    

    【讨论】:

    • 这是我的问题。谢谢!
    【解决方案2】:

    我发现你的问题在于函数onPlayerStateChange() 增量应该在最后(加载视频后):

    function onPlayerStateChange(event) {
            if (event.data === YT.PlayerState.ENDED) {
              console.log(index);
              if (index < videos.length - 1) {
    
                event.target.loadVideoById({
                  videoId: videos[index].vid,
                  startSeconds: videos[index].startSeconds,
                  endSeconds: videos[index].endSeconds
                });
                index++;
            }
        }
    }  
    

    【讨论】:

      【解决方案3】:

      此代码有效,但第一个视频播放了两次,最后一个没有显示。您只需为onPlayerReady 中的第一个视频增加索引,并将&lt;= 放入onPlayerStatechange

      function onPlayerReady(event) {
          event.target.cueVideoById({
              videoId: videos[index],
          });
          event.target.playVideo();
          if (index == 0){
              index++;
          }
      }
      
      // 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.
      ;
      function onPlayerStateChange(event) {
          if (event.data === YT.PlayerState.ENDED) {
              console.log(index);
              if (index <= videos.length - 1) {
      
                  event.target.loadVideoById({
                      videoId: videos[index],
                  });
                  index++;
              }
          }
      }
      

      【讨论】:

        【解决方案4】:
          var index = 0;
           function onPlayerReady(event) {
        
            event.target.cueVideoById({
              videoId: videos[index].vid,
              startSeconds: videos[index].startSeconds,
              endSeconds: videos[index].endSeconds
            });
            event.target.playVideo();
        
          }
        
          function onPlayerStateChange(event) {
            if (event.data === YT.PlayerState.ENDED) {  
              index++;
              if (index > videos.length -1 ) {index=0;}else{index=index;}
        
        
        
                event.target.loadVideoById({
                  videoId: videos[index].vid,
                  startSeconds: videos[index].startSeconds,
                  endSeconds: videos[index].endSeconds
        
                  });
        
        
            }
          }
        

        【讨论】:

        • 请解释你的答案。
        猜你喜欢
        • 2012-04-04
        • 1970-01-01
        • 2012-11-26
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 2016-08-07
        • 2014-07-06
        • 1970-01-01
        相关资源
        最近更新 更多