1

I added this script in my site to reproduce video playlist.

<script type="text/javascript" src="swfobject.js"></script>  
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<script type="text/javascript">
var params = { allowScriptAccess: 'always',
allowFullScreen: 'true' };
var atts = { id: 'myytplayer' };
swfobject.embedSWF("https://www.youtube.com/v/videoseries?listType=playlist&list=PLBA9E733B5C8314DE&autoplay=1&modestbranding=1&enablejsapi=1&playerapiid=ytplayer&version=3", "ytapiplayer", "640", "360", "8", null, null, params, atts);
function onYouTubePlayerReady(myytplayer) {
        ytSwfPlayer = document.getElementById( 'myytplayer' );
        ytSwfPlayer.setShuffle(1);
}
</script>

the setShuffle function don't work!!! You can suggest me a solution?

karmax
  • 171
  • 2
  • 13

3 Answers3

4

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.

Greg Schechter
  • 2,291
  • 15
  • 16
  • I am experiencing the same error and while I implemented the random number solution, it is not optimal as it can repeat a video by selecting the same random number. I'm also having inconsistent results with the playVideoAt function. Is anyone aware of a more robust solution or have knowledge of when the bug will be fixed? – Jon Leach Dec 10 '12 at 16:00
  • the bug seems to be fixed now. note: every time you set a new video or playlist, you need to call setShuffle() again. I think same goes for some other settings, like setLoop() – Sean Jan 13 '16 at 10:50
1

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).

S.C.
  • 2,844
  • 3
  • 26
  • 27
1

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).

Joost
  • 102
  • 8