我有一个正在使用 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&rel=0&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>
如何让视频重播第二、第三、第四次?