Playing YouTube playlist in a randomized order

混江龙づ霸主 提交于 2019-12-11 18:48:35

问题


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

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