我正在尝试使用Youtube API制作歌曲请求页面。我可以将播放列表放入队列中,然后播放,但问题是它继续播放播放列表的第一个视频。 你能给我什么建议吗?
这是我的代码:
var listID = ['dX0WrH0HFgI','iE52-XXnQqs'];
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
// var player;
// function onYouTubeIframeAPIReady(){
// }
window.onYouTubeIframeAPIReady= function() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'fRKq7GOD4hI',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
关于状态更改:
当第一个视频结束时,它将在播放列表中排队,但不会开始播放,因此我添加了一个状态(event.data == -1)来播放列表。
它将重复播放列表中的第一个视频
function onPlayerStateChange(event) {
//when a video ends, queue this playlist
if(event.data == 0){
player.cuePlaylist({
playlist:[listID]
});
}
//when playlist unstarted play the video in playlist
if(event.data == -1){
player.playVideo();
}
}