Youtube iframe API, subscribing to events

微笑、不失礼 提交于 2019-12-06 17:04:34

问题


YouTube iframe API: how do I control a iframe player that's already in the HTML?

How do I subscribe to events here? I would like to listen for video finish.


回答1:


Have a look at my function as defined at this answer: Listening for Youtube Event in JavaScript or jQuery.

In your case, implement the core functions as shown below (Fiddle: http://jsfiddle.net/w2w5x/)

// Core functions defined at https://stackoverflow.com/q/7988536#7988536
var player;
YT_ready(function(){
    var frameID = getFrameID("YOUR-frame-or-container-ID-here");
    if (frameID) { //If the frame exists
        player = new YT.Player(frameID, {
            events: {
                "onStateChange": function(event){
                    if(event.data == "0") {
                        //The video has finished
                        alert("The video has finished!");
                        //Do something, example: play again
                        player.playVideo();
                    }
                }
            }
        });
    }
});


来源:https://stackoverflow.com/questions/7885126/youtube-iframe-api-subscribing-to-events

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