简体   繁体   English

防止 HTML5 视频在移动设备上下载文件 - videojs

[英]Prevent HTML5 videos from downloading the files on mobile - videojs

So I'm currently building a website that has a carousel containing four videos, each video is triggered to play by hooking in to the Bootstrap 3 carousel's slide.bs.carousel event.所以我目前正在构建一个网站,它有一个包含四个视频的轮播,每个视频都是通过连接到 Bootstrap 3 轮播的slide.bs.carousel事件来触发播放的。

Each of the videos are embedded in the page like so:每个视频都嵌入在页面中,如下所示:

<video id="somevideo" class="video-js vjs-default-skin m-hide" controls preload="auto" data-setup='{ "controls": false, "autoplay": false, "preload": "auto" }'>
  <source src="somevideo.mp4">
  <source src="somevideo.webmhd.webm">
</video>

Now, given the restrictions imposed particularly by Apple on the autoplaying and preloading of HTML5 videos (both are disabled and user interaction is required to trigger playback) I've decided to omit the videos for mobile & opt for static images instead.现在,考虑到 Apple 特别对 HTML5 视频的自动播放和预加载施加的限制(两者都被禁用并且需要用户交互才能触发播放),我决定省略移动视频并选择静态图像。 This is relatively simple, since all that's required to stop the video from overlaying the content is a media query that hides them.这相对简单,因为阻止视频覆盖内容所需的只是一个隐藏它们的媒体查询。

That said, I'm finding it very difficult to prevent the videos from being downloaded and the overhead is massive .也就是说,我发现很难阻止下载视频,而且开销很大

For example, I have a script to check whether the user is currently visiting from a mobile device, as such, I've tried:例如,我有一个脚本来检查用户当前是否正在从移动设备访问,因此,我尝试过:

var check = false;
window.mobilecheck = function() {
    // Check for mobile here
    if (check === true) {
        // Device is mobile
        var videos = document.querySelectorAll('.video-js');
        for (var i = 0; i < videos.length; i++) {
            // videojs(videos[i]).destroy();
            videos[i].parentNode.removeChild(videos[i]);
        }
    }
}

This successfully removes the elements, but this has to be called on DOMReady which also means the resources already begin download.这成功地删除了元素,但这必须在 DOMReady 上调用,这也意味着资源已经开始下载。

How do I stop the videos from loading on mobile?如何阻止视频在移动设备上加载? I'd like to find a solution that uses VideoJS inherently preferably.我想找到一个最好使用 VideoJS 的解决方案。

Based on the suggestions Ian kindly made, here is my working solution.根据伊恩好心提出的建议,这是我的工作解决方案。

Firstly, I changed each video's child source elements to have an attribute data-src like so:首先,我将每个视频的子源元素更改为具有如下属性data-src

<video id="my-id">    
   <source data-src="somevideo.mp4">
</video>

Then, after performing a mobile check using the script available at http://detectmobilebrowsers.com/ which I modified to include iPads etc (related SO answer here) I simply used the following script to automatically update the src attribute of each video (if we're on desktop in my case):然后,在使用http://detectmobilebrowsers.com/上提供的脚本执行移动检查后,我修改了该脚本以包含 iPad 等(此处的相关 SO 答案)我只是使用以下脚本自动更新每个视频的src属性(如果就我而言,我们在台式机上):

var sources = document.querySelectorAll('video#my-id source');
// Define the video object this source is contained inside
var video = document.querySelector('video#my-id');
for(var i = 0; i<sources.length;i++) {
  sources[i].setAttribute('src', sources[i].getAttribute('data-src'));
}
// If for some reason we do want to load the video after, for desktop as opposed to mobile (I'd imagine), use videojs API to load
video.load(); 

And that's it!就是这样! Nothing loads on mobile devices anymore and I can have fairly granular control over the devices it will or won't load on.移动设备上不再加载任何内容,我可以对其将加载或不加载的设备进行相当精细的控制。

Hope this helps somebody.希望这可以帮助某人。

One way you could do this is by setting the src attributes of your video element via JavaScript, and only doing so based on a media query (using matchMedia ).一种方法是通过 JavaScript 设置video元素的src属性,并且仅基于媒体查询(使用matchMedia )进行设置。

This would mean that the bulk of your code would have to move to JavaScript though.这意味着您的大部分代码将不得不转移到 JavaScript。

For example, your HTML could be something like:例如,您的 HTML 可能类似于:

<video data-mp4="video.mp4" data-webm="video.webm" class="video-js" controls></video>

And then in your JavaScript (pseudo code here, not actual JS):然后在你的 JavaScript 中(这里是伪代码,不是实际的 JS):

if (window.matchMedia("(min-width: 640px)").matches) {
   // do nothing
} else {
   var videos = document.querySelectorAll('.video-js'),
       videoFile;
   for (var i = 0; i < videos.length; i++) {
      // Choose video type
      if (video[i].canPlayType("video/mp4") === "probably") {
         videoFile = video[i].getAttribute("data-mp4");
      }
      // do the same check for WebM here...
      video[i].src = videoFile;
      // Call whatever reload or refresh method video.js has
      // for example...
      video[i].refresh();
   }
}

Something like that might work for you, although you might have to play around a bit with it.类似的东西可能对你有用,尽管你可能需要玩弄一下。

Based on Ian and GDGR's answers, I modified this to work for multiple videos.根据 Ian 和 GDGR 的回答,我对其进行了修改以适用于多个视频。

<video class="mobile-no-load">    
    <source data-src="somevideo.mp4">
 </video>

 if (window.innerWidth > 730) { // get multiple videos var sources = document.querySelectorAll('video.mobile-no-load'); // loop through the videos sources.forEach(function(source){ // target the src attribute of the <source> element and set it to the data-src attribute source.childNodes[1].setAttribute('src', source.childNodes[1].getAttribute('data-src')) }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM