Youtube iframe API,订阅活动

时间:2011-10-25 05:41:15

标签: javascript events youtube youtube-api youtube-javascript-api

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

如何在此订阅活动?我想听视频结束。

1 个答案:

答案 0 :(得分:0)

查看此答案中定义的我的功能:Listening for Youtube Event in JavaScript or jQuery

在您的情况下,实现核心功能,如下所示(小提琴: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();
                    }
                }
            }
        });
    }
});