我正在尝试使用YouTube API检测视频何时结束,然后调用JS函数,然后再加载另一个。 call函数位于另一个JS文档中,该文档位于我的HTML文档的头部。 这是我到目前为止从API站点获得的代码,但我真的很困惑。
<pre><code> <script>
var ytfullplayer;
function onYouTubePlayerAPIReady() {
console.log('onYouTubePlayerAPIReady');
ytfullplayer = new YT.Player('myVid', {
events: {
if ''onStateChange': getVideo
}
});
}
function getVideo(); }
</script> </code> </pre>
我已经在页面的头部调用了youtube api。但我不是百分百肯定它是否去那里。 任何帮助都会很好。 我正在使用我的YouTube视频。
干杯 马特
答案 0 :(得分:12)
API提供了您在Subscribing to Events下面尝试的示例:
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
function onytplayerStateChange(newState) {
alert("Player's new state: " + newState);
}
将自定义函数绑定为“onStateChange”事件的处理程序,并从内部评估新状态。根据API的“事件”部分,您正在查找状态0
,表示视频已结束。
function onytplayerStateChange(newState) {
if ( newState == 0 ) {
/* load next video */
}
}