다음은 테스트되지 않았습니다. 한 가지 사전 - 임베디드 YouTube iframe과 같은 방식으로 html5 동영상 요소를 처리 할 수 없습니다. 동영상 요소가있는 경우 직접 요소와 상호 작용하고 이벤트 처리기를 사용하여 재생, 일시 중지 및 종료하여 요청을 처리 할 수 있습니다. 가능한 솔루션은 다음과 같이 보일 수 있습니다 : 직접 포함 된 요소를 제어 할 수 없기 때문에, 당신은 youtube API와 함께 작동하도록해야합니다
$(function() {
var refreshId;
// collection of all video elements on your page
var videos = $('video');
// disable reload by clearing the interval
function blockReload() {
clearInterval(refreshId);
}
// your existing reload function
function startReload() {
refreshId = setInterval(function() {
$("#updates").load('updates.php' + '?randval=' + Math.random());
}, 30000);
}
// loop over all video elements on your page and bind
// three event handlers, one for playing, one for pausing
// and one for an ended video.
videos.each(function() {
this.addEventListener('playing', blockReload);
this.addEventListener('paused', startReload);
this.addEventListener('ended', startReload);
});
});
.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '360'
width: '640',
videoId: 'YOURVIDEOID',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
:
// This code loads the IFrame Player API code asynchronously.
// you could also use a normal script tag in that case you would load
// the lib each time.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
다음 스크립트 라이브러리를 통해 비디오를로드 대신 유튜브에서은 iframe을 사용하여, 당신은 (는 API 페이지에서 예) 같은 비디오를로드 할 API를 사용하여
이제 YouTube 동영상과 상호 작용할 수 :
function onPlayerStateChange(event) {
// check if video is running...
if (event.data == YT.PlayerState.PLAYING) {
// disable interval
blockReload();
} else {
// restart interval
startReload();
}
}
봐이 재생됩니다. –