检测YouTube视频播放完成的时间

时间:2013-08-21 15:23:25

标签: javascript youtube-api

我知道这个主题有几个问题。我甚至得到了一个很好的答案here

问题如下。我有一段代码,我无法更改,显示youtube视频。我必须适应它。

问题是onPlayerStateChange()函数永远不会启动。知道为什么会这样吗?

<script src="http://www.youtube.com/player_api"></script>
<script type="text/javascript">
var ytPlayer;
var idplayer;
function onYouTubePlayerReady(playerId) {
    ytPlayer = document.getElementById("ytPlayer");
}

function pauseVideo() {
  if (ytPlayer) {
    ytPlayer.stopVideo();
  }  
}

// when video ends
function onPlayerStateChange(event) {        
    if(event.data === 0) { 
    idplayer=document.getElementById("a_TMBLRWDLH").href;
        window.location.assign(idplayer);
    }
}

j$(document).ready(function(){


    j$('#' + Bnr_vars[0].banner).click(function(){

        if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
{
    var info='<iframe width=\"600\" height=\"338\" src=\"http://www.youtube.com/embed/0Bmhjf0rKe8?rel=0&wmode=transparent&modestbranding=1&autoplay=1&enablejsapi=1\" frameborder=\"0\" allowfullscreen></iframe>';
    j$('#player').html(info);
}

});
</script>

1 个答案:

答案 0 :(得分:0)

我有解决方案,以防万一。我只需要在出现对话框时创建新的YT.Player。

j$(document).ready(function(){
    var Bnr_vars = [
        {
            "banner" : "teamworkRowersDLVidOpener",
            "closer" : "teamworkRowersDLVidCloser",
            "dialog" : "teamworkRowersDLVidDialog",
            "auto"   : "false"              // auto open TRUE or FALSE?
        }
    ];

    lightboxCreate(Bnr_vars); // create lightboxes based on array above 

    j$('#' + Bnr_vars[0].banner).click(function(){

        if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
        {
            var info=new YT.Player('player', {
                height: '338',
                width: '600',
                videoId: '_Zmr0cUeNBY',
                playerVars : {
                    playerapiid:'player1',
                    rel: '0',
                    wmode: 'transparent',
                    modestbranding: '1'
                },
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                }
            });
            j$('#player').html(info);
        }

    });