所以基本上有一个按钮,当点击时会打开并全屏播放视频。我想在用户退出全屏时停止视频,但无法捕捉到该事件。
到目前为止,我将其用作http://codepen.io/bfred-it/pen/GgOvLM
有人可以指导我找到可能的解决方案。
HTML:
<script src="https://www.youtube.com/iframe_api"></script>
<button>play fullscreen</button><br>
<div id="player"></div>
JS:
var player, iframe;
var $ = document.querySelector.bind(document);
// init player
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '200',
width: '300',
videoId: 'dQw4w9WgXcQ',
events: {
'onReady': onPlayerReady
}
});
}
// when ready, wait for clicks
function onPlayerReady(event) {
var player = event.target;
iframe = $('#player');
setupListener();
}
function setupListener (){
$('button').addEventListener('click', playFullscreen);
}
function playFullscreen (){
player.playVideo();//won't work on mobile
var requestFullScreen = iframe.requestFullScreen || iframe.mozRequestFullScreen || iframe.webkitRequestFullScreen;
if (requestFullScreen) {
requestFullScreen.bind(iframe)();
}
}
答案 0 :(得分:5)
您可以检测浏览器何时full screen change,然后停止视频
document.addEventListener("fullscreenchange", function() {
if (!document.fullscreenElement) player.stopVideo();
}, false);
document.addEventListener("msfullscreenchange", function() {
if (!document.msFullscreenElement) player.stopVideo();
}, false);
document.addEventListener("mozfullscreenchange", function() {
if (!document.mozFullScreen) player.stopVideo();
}, false);
document.addEventListener("webkitfullscreenchange", function() {
if (!document.webkitIsFullScreen) player.stopVideo();
}, false);