I have a html file that contains a video. I want to add a feature to pause video when the window is minimized or the tab is changed on browser.
How can add that feature to my html?
I added javascript function but video is still being played in any condition
edit: sharing my code.
<!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>
Simple javascript event:
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 event with 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
Maybe this can be useful for you to detect when the page loses focus or visibility: 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 to detect if unfocused:
//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();
};
}
};
})
Don't hesitate to comment my answer, if you need further help with that.
You need a combination of two things -
The first being the ability to programatically pause a video using javascript.
HTML
<video id="videoContainer" src="videoLink.ogg"></video>
JavaScript
document.getElementById('videoContainer').pause();
The next step would be to detect if the window is in focus or not. For a complete answer you should look at the answer to Is there a way to detect if a browser window is not currently active?
But the code that is posted there is setup for browser comparability, and I'm not sure how much of that you need, so I would suggest reading through it and taking what you need from it.
(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"});
})();
来源:https://stackoverflow.com/questions/36430064/html-video-pause-when-window-is-minimized