HTML 视频在 window 最小化时暂停
HTML video pause when window is minimized
我有一个包含视频的 html 文件。我想添加一项功能以在 window 最小化或浏览器上的选项卡更改时暂停视频。
如何将该功能添加到我的 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>
也许这对您检测页面何时失去焦点或可见性很有用:Javascript to detect if user changes tab
$(window).focus(function() {
//do something
});
$(window).blur(function() {
//do something
});
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();
};
}
};
})
如果您需要进一步的帮助,请随时评论我的回答。
你需要两个东西的组合 -
首先是使用 javascript 以编程方式暂停视频的能力。
HTML
<video id="videoContainer" src="videoLink.ogg"></video>
JavaScript
document.getElementById('videoContainer').pause();
下一步是检测 window 是否对焦。要获得完整答案,您应该查看 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"});
})();
简单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>
Javascript 带有 EventListener 的事件:
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>
video source: techslides
我有一个包含视频的 html 文件。我想添加一项功能以在 window 最小化或浏览器上的选项卡更改时暂停视频。
如何将该功能添加到我的 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>
也许这对您检测页面何时失去焦点或可见性很有用:Javascript to detect if user changes tab
$(window).focus(function() {
//do something
});
$(window).blur(function() {
//do something
});
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();
};
}
};
})
如果您需要进一步的帮助,请随时评论我的回答。
你需要两个东西的组合 -
首先是使用 javascript 以编程方式暂停视频的能力。
HTML
<video id="videoContainer" src="videoLink.ogg"></video>
JavaScript
document.getElementById('videoContainer').pause();
下一步是检测 window 是否对焦。要获得完整答案,您应该查看 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"});
})();
简单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>
Javascript 带有 EventListener 的事件:
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>
video source: techslides