修改数组不随机播放

时间:2017-02-19 22:53:36

标签: javascript jquery

我试图不以随机顺序播放,只是按照数组的顺序播放并循环播放。有谁知道如何修改它?

我对javascript很新,很抱歉,如果这个问题甚至可以问哈哈。

感谢您的帮助!

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

<script src="http://www.youtube.com/player_api"></script>

<script>

    var videos = [
      '3H3odKtfrTo',
      'BxjMGiN0jJY',
      'iGC9n0NAvDU'
    ]


    var index=Math.floor(Math.random() * videos.length);

    // create youtube player
    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: '9yT7KcHCrRY',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
    }

    // autoplay video
    function onPlayerReady(event) {
        event.target.playVideo();
    }

    function playNewVideo(id){
      player.loadVideoById(videos[index]);
      event.target.playVideo();
      playNewVideo(randomID)
    }

    // when video ends
    function onPlayerStateChange(event) {        
        if(event.data === 0) { 
          //generate new random number
          index=Math.floor(Math.random() * videos.length);
          playNewVideo();
        }
    }

</script>

1 个答案:

答案 0 :(得分:0)

只是做:

// In the beginning
var index = 0
// After video is done playing.
index += 1
if(index < videos.length) { // If its not the last item on the list
    playNewVideo()
} else {
    // Go back to the start
    index = 0
    playNewVideo()
}

如果您想开始随机视频,然后根据列表进行播放:

// In the beginning
var index = Math.floor(Math.random() * videos.length)