我会撒谎开始使用youtube API完成另一个视频播放器。但是当我按照文档并尝试通过“onStateChange”进行操作时,第二个玩家会继续循环。
我知道通过使用它,使函数全局,但如何隔离这个函数,所以它只会听'player1的ENDED状态?
谢谢!
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
slide4();
}
}
//slide 2
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: '820',
width: '707',
videoId: 'NTq1WLKuOI4',
playerVars: {'controls':1, 'rel':0,},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player2 = new YT.Player('player2', {
height: '820',
width: '707',
videoId: 'AQx1UjLv3Ps',
playerVars: {'controls':1, 'rel':0,},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player3 = new YT.Player('player3', {
height: '820',
width: '707',
videoId: 'D4zRMA28UGI',
playerVars: {'controls':1, 'rel':0,},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
player.playVideo();
}
// 5. The API calls this function when the player's state changes.
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
slide4();
}
}
//slide 4
var slide4 = function(){
player2.playVideo();
}