javascript youtube api 视频结束加载功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10459622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:57:10  来源:igfitidea点击:

youtube api end of video load a function

javascriptjqueryhtmlapiyoutube

提问by Matt Law

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.

我正在尝试使用 YouTube API 来检测视频何时结束,然后调用一个 JS 函数,然后我必须加载另一个。调用函数位于另一个 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>

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.

我在我的页面的头部调用了 youtube api。但我不是 100% 确定它是否会去那里。任何帮助都会很好。我正在为我的 youtube 视频使用 。

Cheers Matt

干杯马特

回答by Sampson

The API provides an example of what you're attempting beneath Subscribing to Events:

API 提供了一个示例,说明您在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.

将您的自定义函数作为处理程序绑定到“onStateChange”事件,并从内部评估新状态。根据 API 的 Events 部分,您正在寻找 state 0,表明视频已结束。

function onytplayerStateChange(newState) {
   if ( newState == 0 ) {
     /* load next video */
  }
}