youtube API - setShuffle don't work

两盒软妹~` 提交于 2019-12-08 04:23:52

问题


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?


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/12916017/youtube-api-setshuffle-dont-work

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