youtube api视频结束加载功能 [英] youtube api end of video load a function
问题描述
我正在尝试使用 YouTube API 来检测视频何时结束,然后调用一个 JS 函数,然后我必须加载另一个.调用函数位于另一个 JS 文档中,该文档位于我的 HTML 文档的头部.这是我目前从 API 站点获得的代码,但我真的很困惑.
<脚本>var ytfullplayer;函数 onYouTubePlayerAPIReady() {console.log('onYouTubePlayerAPIReady');ytfullplayer = new YT.Player('myVid', {事件:{如果 ''onStateChange': getVideo}});}函数 getVideo();}
我在页面顶部调用了 youtube api.但我不是 100% 确定它是否会去那里.任何帮助都会很好.我正在为我的 youtube 视频使用 .
干杯马特
API 在 订阅活动:
函数 onYouTubePlayerReady(playerId) {ytplayer = document.getElementById("myytplayer");ytplayer.addEventListener("onStateChange", "onytplayerStateChange");}功能 onytplayerStateChange(newState) {alert("玩家的新状态:" + newState);}
将您的自定义函数作为处理程序绑定到onStateChange"事件,并从内部评估新状态.根据 API 的事件部分,您正在寻找状态 0
,表示视频已结束.
function onytplayerStateChange(newState) {如果(新状态== 0){/* 加载下一个视频 */}}
I am trying to use the YouTube API to detect when a video ends and then to call a JS function I have to then load another one. The call function is in another JS document which is in the head of my HTML document. This is the code I have so far from the API site but I am really confused with it.
<pre><code> <script>
var ytfullplayer;
function onYouTubePlayerAPIReady() {
console.log('onYouTubePlayerAPIReady');
ytfullplayer = new YT.Player('myVid', {
events: {
if ''onStateChange': getVideo
}
});
}
function getVideo(); }
</script> </code> </pre>
I have called the youtube api in the head of my page. But I am not 100% sure if it goes there. Any assistance would be nice. I am using an for my youtube video.
Cheers Matt
The API provides an example of what you're attempting beneath Subscribing to Events:
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
function onytplayerStateChange(newState) {
alert("Player's new state: " + newState);
}
Bind your custom function as the handler to the "onStateChange" event, and evaluate the new state from within. According to the Events portion of the API, you're looking for state 0
, indicating the video has ended.
function onytplayerStateChange(newState) {
if ( newState == 0 ) {
/* load next video */
}
}
这篇关于youtube api视频结束加载功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!