4

我一直在尝试设置我的 youtube api 来随机播放我的播放列表,但顺序始终相同......

这是我从 https://developers.google.com/youtube/iframe_api_reference#setShuffle获取代码的链接

我说的对吗?

这是我下面的代码

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

<script>

  var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

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

    });
  }



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

  }

</script>

我尝试将 shufflePlaylist 设置为“true”,我尝试过 cueplaylist 而不是 loadplaylist,我什至尝试将 shufflePlaylist 放入加载播放列表中......没有任何东西被洗牌。

我对此进行了调查,发现这是 API 的问题,但那是 2 年前的事了,有没有解决这个问题的方法?

以下是我尝试过但没有奏效的方法

player.setShuffle(1);
        player.setShuffle('1');
        player.setShuffle(true);
        player.setShuffle('true');
        player.setShuffle({'shufflePlaylist' : true});
        player.setShuffle({'shufflePlaylist' : 1});
        player.setShuffle({'shufflePlaylist' : 'true'});
        player.setShuffle({'shufflePlaylist' : '1'});

I have also tried copying and example from here....https://developers.google.com/youtube/youtube_player_demo

The Shuffle works here after u load the playlist and click a button.

4

4 回答 4

3

This is a known bug that is yet to be fixed - see this post

A work around however is to pass a random number to playVideoAt, as follows:

var getRandom = function(min, max) {
  return Math.random() * (max - min) + min;
};

var playRandomTrack = function() {
  num = getRandom(0, 99);
  player.playVideoAt(num);  
};
于 2013-08-14T23:51:36.140 回答
1

I found the solution. You have to load the player.setShuffle(true) just after the first video starts:

 function onPlayerReady(event) {
    event.target.playVideo();
    setTimeout(setShuffleFunction, 1000);           
  }
  function setShuffleFunction(){
    player.setShuffle(true);
  }

Demo on my online music tv website

于 2016-01-19T16:26:08.313 回答
0

More or less the same solution as Ivan's. Works for me!

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

于 2016-02-05T17:20:02.563 回答
0

Try the following:

function onPlayerStateChange(event) {
    if(event.data === 0) {
        player.setShuffle(true);
        player.playVideo();
    }
}
于 2019-02-28T14:01:35.203 回答