4

我有一个包含视频的 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>
4

5 回答 5

3

简单的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

于 2016-04-05T15:37:00.127 回答
0

也许这对您检测页面何时失去焦点或可见性很有用:Javascript to detect if user changes tab

于 2016-04-05T15:05:06.870 回答
0

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

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

于 2016-04-05T15:07:11.700 回答
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();
     };
   }
  };  
})

如果您需要进一步的帮助,请随时评论我的答案。

于 2016-04-05T15:11:30.327 回答
0

你需要结合两件事 -

第一个是使用 javascript 以编程方式暂停视频的能力。

HTML

<video id="videoContainer" src="videoLink.ogg"></video>

JavaScript

document.getElementById('videoContainer').pause();

下一步是检测窗口是否在焦点上。要获得完整的答案,您应该查看是否有办法检测浏览器窗口当前是否处于非活动状态?

但是那里发布的代码是为浏览器可比性设置的,我不确定你需要多少,所以我建议你通读它并从中获取你需要的东西。

(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"});
})();
于 2016-04-05T15:12:35.467 回答