youtube API - setShuffle don't work

可紊 提交于 2019-12-08 02:14:26

This appears to be a bug in the player. I've reported this bug to the team. In the future you can report bugs at https://code.google.com/p/gdata-issues/issues/entry?template=YouTube%20(Defect%20Report)

As a work around you could use the JS api to shuffle it yourself. When a video ends you can call playVideoAt and pass a random number.

The problem is that if you've loaded a playlist queue in the Youtube player, the next track will automatically start at the end of the current playing one.

So you should bind track ended event (which is event.data = 0) and then make the youtube player make two things:

  1. stop playing
  2. play a new track with a random index by calling playVideoAt method.

A simple solution to avoid repeating the same track more times is to store the played idx in a list. Then, you should have a function, like shuffle that generates a random integer between 0 and the queue length:

function getRandomId() {
    var random_id = 0
    while(played_idx.indexOf(random_id) != -1) {
      random_id = Math.floor(Math.random * playlist.length)
    }
    return random_id
}

Then, simply call playVideoAt(random_id).

I've noticed that setShuffle does work if you launch the command a little later. Something like this:

function onPlayerReady(event) {
    event.target.mute();
    setTimeout( function() { 
        event.target.setShuffle(true); 
        event.target.setLoop(true);
    }, 2000);
}

If you launch it directly, it will definitely not work (I have noticed, to my frustration).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!