Skip to content Skip to sidebar Skip to footer

Html Video Pause When Window Is Minimized

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?

Solution 1:

Simple javascript event:

window.onfocus = function() { document.getElementById('player').play(); };
window.onblur = function() { document.getElementById('player').pause(); };
<videoheight="180"controlsautoplayloopid=player><sourcesrc="http://techslides.com/demos/sample-videos/small.mp4"type="video/mp4"><sourcesrc="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);

functionaaa(){
document.getElementById('player').play();  
}

functionbbb(){
document.getElementById('player').pause();
}
<videoheight="180"controlsautoplayloopid=player><sourcesrc="http://techslides.com/demos/sample-videos/small.mp4"type="video/mp4"><sourcesrc="http://techslides.com/demos/sample-videos/small.ogv"type="video/ogg">
  Your browser does not support HTML5 video.
</video>

video source: techslides

Solution 2:

Maybe this can be useful for you to detect when the page loses focus or visibility: Javascript to detect if user changes tab

Solution 3:

$(window).focus(function() {
    //do something
});

$(window).blur(function() {
    //do something
});

Solution 4:

HTML

<scriptsrc="//code.jquery.com/jquery-1.11.1.min.js"></script><divid="youtube"></div><pid="status"></p>

Javascript to detect if unfocused:

//YouTubevar 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 = newYT.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.

Solution 5:

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 indocument)
    document.addEventListener("visibilitychange", onchange);
  elseif ((hidden = "mozHidden") indocument)
    document.addEventListener("mozvisibilitychange", onchange);
  elseif ((hidden = "webkitHidden") indocument)
    document.addEventListener("webkitvisibilitychange", onchange);
  elseif ((hidden = "msHidden") indocument)
    document.addEventListener("msvisibilitychange", onchange);
  // IE 9 and lower:elseif ("onfocusin"indocument)
    document.onfocusin = document.onfocusout = onchange;
  // All others:elsewindow.onpageshow = window.onpagehide
    = window.onfocus = window.onblur = onchange;

  functiononchange (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.typein evtMap)
      document.body.className = evtMap[evt.type];
    elsedocument.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"});
})();

Post a Comment for "Html Video Pause When Window Is Minimized"