播放YouTube视频时停止jQuery内容滑块

时间:2010-12-01 14:15:12

标签: jquery video youtube youtube-api

我正在尝试创建一个jQuery内容滑块,该视频将在播放YouTube视频时停止,并在视频停止时恢复。与taprootfoundation.org上的滑块类似。

YouTube JavaScript API中的

player.getPlayerState()返回播放器的状态。未启动的视频应返回值-1。

我的下面的脚本使用return false;如果getPlayerState()返回-1以外的任何值,则停止幻灯片放映。取下面代码中的底部块(然后//检测播放YouTube视频),它工作正常,但目前“返回false”;无论getPlayerState()值如何都在触发。

我使用swfobject嵌入播放器,正如YouTube JavaScript API中所推荐的那样。这是一个screenshot showing the HTML of the embedded player

我必须错误地调用.getPlayerState(),但我不确定如何纠正它。

$(document).ready(function(){
//initial setup
    //adds class "last" to last link-tab item
    $('.link-tab:last').addClass('last');

    //moves summary elements down out of view
    $('.rotate-image > div').children('.summary').css({bottom:-150});

//initial variables
    var captionAnimationTime = 300;
    var captionPauseTime = 4800;
    var slideTime = 6000;

//detect playing YouTube video
/*  window.setTimeout(function(){
            video = document.getElementById("boundless-playground-video");
    }
    , captionAnimationTime);
*/
//main function
slideSwitch = function(){
    var $active = $('.rotate-image > div.active');

    if ($active.length == 0) $active = $('.rotate-image > div:last');

    var $next = $active.next().length ? $active.next() : $('.rotate-image > div:first');

    //sets indexCurrent variable as the index of the next active banner item in the slideshow
    var indexCurrent = $next.prevAll().length;

    //removes "active" class from link-tab items that already have it
    $('.link-tab.active').removeClass('active');
    //gives class "active" to next link-tab item
    $('.link-tab').eq(indexCurrent).addClass('active');

    $active.addClass('last-active');

    //function to slide down the caption
    captionDown = function(){
        $next.children('.summary').animate({bottom:-150}, captionAnimationTime);
    }

    $next.css({opacity:0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 700, function(){
            //remove classes from the previously active item
            $active.removeClass('active last-active');

            //animates slide of summary element
            $next.children('.summary').animate({bottom: 0}, captionAnimationTime);
            window.setTimeout(captionDown, captionPauseTime);
        });

    //detect playing YouTube video - currently stopping regardless of player state
    video = document.getElementById("boundless-playground-video");
    if(video.getPlayerState() != -1){
        return false;
    }
};

//run the function once on document ready to show first slide
slideSwitch();

$(function(){
    setInterval( "slideSwitch()", slideTime);
});

});

1 个答案:

答案 0 :(得分:2)

经过多次拉扯后解决了这个问题。将YouTube API调用放在

中时,我遇到了范围问题
jQuery $(document).ready(function(){
});

我所做的是将所谓的YouTube API调用放在jQuery文档的顶部。

//playerState variable shows status of YouTube video. 
//see http://code.google.com/apis/youtube/youtube_player_demo.html
//currently hardcoded with id of video
function onYouTubePlayerReady(){
  ytplayer = document.getElementById('boundless-playground-video');
  ytplayer.addEventListener('onStateChange','onytplayerStateChange');
}
function onytplayerStateChange(newState) {
  playerState = newState;
}

$(document).ready(function(){
//insert other stuff here
if (typeof (playerState) == 'undefined'){
window.playerState = 5;
}
alert(window.playerState);
});