使用 iframe youtube API 我想要这个 if else 语句来播放这个播放列表中的某个随机视频给定选择的数字。到目前为止,它似乎给了我一些不是基于生成的随机数的重复视频。
<script type="text/javascript">
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',
playerVars: {},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
'nextVideo': nextVideo,
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
player.loadPlaylist({
list: 'mozart',
listType:'search',
})
event.target.playVideo();
}
function nextVideo() {
var num = Math.round(Math.random() * 4);
console.log(num);
if (num >= 2) {
player.nextVideo();
player.loadPlaylist({
list: '90s rap',
listType: 'search',
})
player.setShuffle(true);
player.playVideo();
}
else if (num < 2) {
player.nextVideo();
player.loadPlaylist({
list: 'mozart',
listType:'search',
})
player.setShuffle(true);
player.playVideo();
}
else {}
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
function onPlayerStateChange(event) {
}
function stopVideo() {
player.stopVideo();
}
</script>
这是html
<a href="javascript:nextVideo()" id="next">Next</a>