Skip to content

Instantly share code, notes, and snippets.

@swannknani
Last active August 29, 2015 14:02
Show Gist options
  • Save swannknani/de9cc22ce39112da760d to your computer and use it in GitHub Desktop.
Save swannknani/de9cc22ce39112da760d to your computer and use it in GitHub Desktop.
Force preload of video in HTML5
function addSourceToVideo(element, src, type) {
var source = document.createElement('source');
source.src = src;
source.type = type;
element.appendChild(source);
}
var video;
var index = 0;
$(document).ready(function(){
video = document.getElementsByTagName('video')[0];
addSourceToVideo( video, "http://video-js.zencoder.com/oceans-clip.ogv", "video/ogv");
addSourceToVideo( video, "http://video-js.zencoder.com/oceans-clip.mp4", "video/mp4");
video.addEventListener("progress", progressHandler,false);
bindKeys();
});
progressHandler = function(e) {
if( video.duration ) {
var percent = (video.buffered.end(0)/video.duration) * 100;
console.log( percent );
if( percent >= 100 ) {
console.log("loaded!");
}
video.currentTime++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment