我有一个包含视频的html文件。我希望添加一项功能,以便在窗口最小化或在浏览器上更改选项卡时暂停视频。
如何将该功能添加到我的HTML?
我添加了javascript功能,但视频仍然在任何条件下播放
编辑:分享我的代码。
<!DOCTYPE html>
<html>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<video id="videoPlayer" width="640" height="480" position="center" controls>
<source src="\\TEKPC1366\Users\BerkayS\Desktop\DataModel2\DataModel3\DataModelYazisiz.mp4" type="video/mp4" />
<source src="\\TEKPC1366\Users\BerkayS\Desktop\DataModel2\DataModel3\DataModelYazisiz.mp4.webm" type="video/webm" />
<track src="C:\Users\BerkayS\Desktop\DataModel2\DataModel3\deneme.vtt" kind="subtitles" srclang="tu" label="Türkçe" />
<track src="C:\Users\BerkayS\Desktop\DataModel2\DataModel3\denemeenglish.vtt" kind="subtitles" srclang="en" label="English" />
<track src="C:\Users\BerkayS\Desktop\DataModel2\DataModel3\denemedeutsch.vtt" kind="subtitles" srclang="de" label="Deutsch" />
<track src="C:\Users\BerkayS\Desktop\DataModel2\DataModel3\denemespanish.vtt" kind="subtitles" srclang="es" label="Espanol" />
<track src="C:\Users\BerkayS\Desktop\DataModel2\DataModel3\denemerussian.vtt" kind="subtitles" srclang="ru" label="русский" />
<track src="C:\Users\BerkayS\Desktop\DataModel2\DataModel3\denemearabic.vtt" kind="subtitles" srclang="ar" label="العربية" />
</video>
<script
window.onfocus = function() {kaf()};
window.onblur = function() {kef()};
function kaf() {
document.getElementById('videoPlayer').play();
}
function kef() {
document.getElementById('videoPlayer').pause();
}
</script>
</body>
</html>
答案 0 :(得分:2)
简单的javascript事件:
window.onfocus = function() { document.getElementById('player').play(); };
window.onblur = function() { document.getElementById('player').pause(); };
<video height="180" controls autoplay loop id=player>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
<source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
Your browser does not support HTML5 video.
</video>
使用EventListener的Javascript事件:
window.addEventListener("focus", aaa);
window.addEventListener("blur", bbb);
function aaa(){
document.getElementById('player').play();
}
function bbb(){
document.getElementById('player').pause();
}
<video height="180" controls autoplay loop id=player>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
<source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
Your browser does not support HTML5 video.
</video>
视频来源:techslides
答案 1 :(得分:0)
也许这可以帮助您检测页面何时失去焦点或可见性:Javascript to detect if user changes tab
答案 2 :(得分:0)
$(window).focus(function() {
//do something
});
$(window).blur(function() {
//do something
});
答案 3 :(得分:0)
<强> HTML 强>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<div id="youtube"></div>
<p id="status"></p>
Javascript 以检测是否未聚焦:
//YouTube
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
$(function(){
var youtubePlayer;
var myWindow = window;
onYouTubeIframeAPIReady = function(){
youtubePlayer = new YT.Player('youtube', {
height: '390',
width: '640',
videoId: 'sXtekwuT8R0',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
onPlayerReady = function(event) {
event.target.playVideo();
};
onPlayerStateChange = function(event) {
if (event.data == YT.PlayerState.PLAYING) {
$("#status").text("Playing!");
myWindow.focus();
myWindow.onblur = function() {
$("#status").text("You went away!");
youtubePlayer.stopVideo();
};
}
};
})
如果您需要进一步的帮助,请不要对我的回答发表评论。
答案 4 :(得分:0)
你需要两件事的组合 -
第一种是使用javascript以编程方式暂停视频的功能。
<强> HTML 强>
<video id="videoContainer" src="videoLink.ogg"></video>
<强>的JavaScript 强>
document.getElementById('videoContainer').pause();
下一步是检测窗口是否对焦。要获得完整的答案,您应该查看Is there a way to detect if a browser window is not currently active?
的答案但是在那里发布的代码是浏览器可比性的设置,我不确定你需要多少,所以我建议你阅读它并从中获取你需要的东西。
(function() {
var hidden = "hidden";
// Standards:
if (hidden in document)
document.addEventListener("visibilitychange", onchange);
else if ((hidden = "mozHidden") in document)
document.addEventListener("mozvisibilitychange", onchange);
else if ((hidden = "webkitHidden") in document)
document.addEventListener("webkitvisibilitychange", onchange);
else if ((hidden = "msHidden") in document)
document.addEventListener("msvisibilitychange", onchange);
// IE 9 and lower:
else if ("onfocusin" in document)
document.onfocusin = document.onfocusout = onchange;
// All others:
else
window.onpageshow = window.onpagehide
= window.onfocus = window.onblur = onchange;
function onchange (evt) {
var v = "visible", h = "hidden",
evtMap = {
focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
};
evt = evt || window.event;
if (evt.type in evtMap)
document.body.className = evtMap[evt.type];
else
document.body.className = this[hidden] ? "hidden" : "visible";
}
// set the initial state (but only if browser supports the Page Visibility API)
if( document[hidden] !== undefined )
onchange({type: document[hidden] ? "blur" : "focus"});
})();