I've written a custom media preloader which uses a series of XMLHttpRequests
to load large amounts of media prior to displaying my ng2 app
. It is a stakeholder requirement that all media is downloaded in full prior to use of the 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();
})
}
It works great and successfully loads in all the media that I feed it.
I only have 1 issue and that is in Chrome, when I reference a <video>
or <audio>
which has been pre-loaded,, it doesn't pull it from the cache, it instead re-downloads it from the server. (IE9 even pulls from cache)
Any audio and video elements will always re-download from the server...
<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>
This will always load from cache...
<img src="./app/assets/images/BTFG-BOLD_Fundamentals.png" />
Here are 2 screenshots, one from chrome and one from edge showing the network activitiy from the dev tools (both had their caches reset prior to launch)...
The main difference that I notice is the request status is different between browsers when it comes to rendering the content (post preloading). But why is this the case?
I found this SO post from 2013 which states that:
How video is buffered is browser implementation dependent and therefor may vary from browser to browser.
Various browsers can use different factors to determine to keep or to discard a part of the buffer. Old segments, disk space, memory and performance are typical factors.
Is this what is happening here? And if so, does anyone know a way to fix this so that chrome always attempts to pull the videos from cache?