【发布时间】:2017-02-01 23:52:19
【问题描述】:
我编写了一个自定义媒体预加载器,它使用一系列XMLHttpRequests 在显示我的ng2 app 之前加载大量媒体。利益相关者要求所有媒体在使用应用程序之前完整下载。
private loadFile(media: any) {
return new Promise(function (resolve, reject) {
var error: boolean = false;
//for (var media of media.videos) {
//TODO: Check how this loads.....
//console.log("Now Loading video >> ", media, media.hasOwnProperty("path"));
// Standard XHR to load an image
var request = new XMLHttpRequest();
request.open("GET", (<any>media).path);
request.responseType = 'blob';
// When the request loads, check whether it was successful
request.onload = () => {
if (request.status === 200) {
resolve(request.response);
}
else
// If it fails, reject the promise with a error message
reject(Error('Media didn\'t load successfully; error code:' + request.statusText));
};
// If an error occurs
request.onerror = () => {
// Also deal with the case when the entire request fails to begin with
// This is probably a network error, so reject the promise with an appropriate message
reject(Error('There was a network error.'));
};
request.onreadystatechange = function () {
if (request.readyState == 4) {
console.log("Load Complete >> ", media, "from", request.status); // Another callback here
}
};
// Every tick of the progress loader
request.onprogress = data => console.log(data);
// Send the request
request.send();
})
}
它运行良好并成功加载到我提供给它的所有媒体中。
我只有 1 个问题,那就是在 Chrome 中,当我引用已预加载的 <video> 或 <audio> 时,它不会从缓存中提取它,相反,它会从服务器重新下载它。 (IE9 甚至从缓存中提取)
任何音频和视频元素将始终从服务器重新下载...
<video width="640px" height="auto" controls autoplay preload="auto">
<source src="./app/assets/video/Awaiting%20Video%20Master.mov" type="video/mp4"/>
</video>
<audio controls autoplay preload="auto">
<source src="./app/assets/audio/1_2_4_audio1.mp3" type="audio/mp3" />
</audio>
这将始终从缓存中加载...
<img src="./app/assets/images/BTFG-BOLD_Fundamentals.png" />
这里有 2 张屏幕截图,一张来自 chrome,一张来自 edge,显示来自开发工具的网络活动(两者都在启动前重置了缓存)...
我注意到的主要区别是在渲染内容(预加载后)时,浏览器之间的请求状态是不同的。但是为什么会这样呢?
我发现 this SO 2013 年的帖子指出:
视频的缓冲方式取决于浏览器的实现,因此可能因浏览器而异。
各种浏览器可以使用不同的因素来决定保留或丢弃缓冲区的一部分。旧段、磁盘空间、内存和性能是典型因素。
这就是这里发生的事情吗?如果是这样,是否有人知道解决此问题的方法,以便 chrome 始终尝试从缓存中提取视频?
【问题讨论】:
标签: javascript html google-chrome caching