youtube api end of video load a function

天大地大妈咪最大 提交于 2019-12-21 11:30:38

问题


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


回答1:


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 */
  }
}


来源:https://stackoverflow.com/questions/10459622/youtube-api-end-of-video-load-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!