5

我有一个正在使用 YouTubeAPI ( iframe ) 播放的视频。在单击控件开始播放视频之前,图像会代替视频。图像与视频交换并播放视频。当视频结束时,该过程是相反的。问题是我可以使用自定义控件重新启动视频。这是我的代码...

<script src="https://www.youtube.com/iframe_api"></script>

    <div class="video" id="vid">
        <a class="video-control" href="#"></a>
        <img id="video-front" src="<?= get_field('video_image') ?>" class="img-responsive" alt="">
        <div class="video-container">
            <iframe id="player" type="text/html"
src="http://www.youtube.com/embed/G5M721A0b_Q?enablejsapi=1&amp;rel=0&amp;autoplay=1"
frameborder="0"></iframe>
        </div>
    </div>
<script>
(function($){

//move vid control
vidControlHeight();



$('.video-control').css('left', '-' + wControl + 'px');

$('.video-control').on('click', function(event){
        event.preventDefault();

            $('#video-front').hide();
            $('.video-container').show();

            onYouTubeIframeAPIReady();

})

})(jQuery);

function vidControlHeight(){
var hBox        = $('#vid').height() / 2,
    hControl    = $('.video-control').height() / 2,
    boxPosition = hBox - hControl;
    wControl    = $('.video-control').width() / 2;


$('.video-control').css({'left':'-' + wControl + 'px', 'top': boxPosition + 'px'});
}
var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

   function onPlayerReady(event) {
    event.target.playVideo();
  }

  function onPlayerStateChange(event) {

    if(event.data == YT.PlayerState.ENDED){
        $('#video-front').show();
        $('.video-container').hide();

    }

  }
</script>

如何让视频重播第二、第三、第四次?

4

2 回答 2

10

尽管它正在调用播放,但视频并没有回到开头。我不得不设置

player.seekTo(0);

在 onPlayerStateChange() 函数中。

function onPlayerStateChange(event) {

    if(event.data == YT.PlayerState.ENDED){

        player.seekTo(0);
        $('#video-front').show();
        $('.video-container').hide();

    }

  }

https://developers.google.com/youtube/js_api_reference#seekTo

于 2013-11-18T11:41:19.247 回答
0

您可以添加一个loop参数:

function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
        },
        loop: 1
    });
}

https://developers.google.com/youtube/player_parameters#loop

或者你可以检查你onPlayerStateChange的状态是否0是视频结束的时候,然后打电话event.target.playVideo();

于 2013-11-18T11:26:13.763 回答