YouTube API视频完成后

时间:2013-04-08 06:43:32

标签: javascript events youtube-api

我正在使用YouTube API,我正在尝试设置onStateChange,当我的视频结束以随机播放我的播放列表并从0开始...这是我到目前为止所得到的

 var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }

        });
      }





      function onPlayerReady(event) {
        player.loadPlaylist({'listType': 'playlist', 'list': 'PLF776D41D8211F340','index': '0','startSeconds': '0','suggestedQuality': 'hd720'});
      }

      var done = false;
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING && done == false) {
            player.setShuffle(true);
            player.playVideoAt(0);
          done = true;
        }else if (event.data == YT.PlayerState.ENDED && done == true) {
            player.setShuffle(true);
            player.playVideoAt(0);
          done = false;
        }

      }

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要在player.playVideoAt(0)功能中加入onPlayerReady

function onPlayerReady(event) {
    player.loadPlaylist({'listType': 'playlist', 'list': 'PLF776D41D8211F340','index': '0','startSeconds': '0','suggestedQuality': 'hd720'});
    player.playVideo();
  }

另外,当您播放播放​​列表中的第一个视频时,为什么不使用player.playVideo()代替。

我没有包含player.setShuffle(true),因为它目前无效(已知错误) - 请参阅此post