问题
I was having issues getting it to work so that the playlist, every time the page is refreshed, the playlist would play in a randomized order.
Code: https://jsfiddle.net/qnbyg5x9/92/
This is what I tried, it works, but I don't know if I'm doing it right, or if there's a better way to do it.
function onPlayerReady(event) {
const youtubePlayer = event.target;
youtubePlayer.setVolume(0); // percent
}
let hasShuffled = false;
function onPlayerStateChange(event) {
const player = event.target;
if (!hasShuffled) {
player.setShuffle(true);
player.playVideoAt(0);
hasShuffled = true;
}
}
window.onYouTubePlayerAPIReady = function() {
new YT.Player(document.querySelector(".js-player"), {
height: '315',
width: '560',
host: 'https://www.youtube-nocookie.com',
playerVars: {
autoplay: 0,
controls: 1,
listType: 'playlist',
list: 'TLGGCePU6hOj6WQyMjEyMjAxOA'
},
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
});
};
What's supposed to happen is, every time the page is refreshed, the songs reset themselves in a randomized order, and it plays in that order.
It does this now, but I don't know if I'm doing it right, or if there's a better way to do it.
来源:https://stackoverflow.com/questions/53905846/playing-youtube-playlist-in-a-randomized-order