HTML video pause when window is minimized

≡放荡痞女 提交于 2019-12-04 14:20:18

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

rubentd

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.

Adjit

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"});
})();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!