我即将尝试在HTML5中完全预加载视频。我使用属性preload=auto
但它没有预加载整个视频...在Chrome中,视频只预装了2%,Safari大约50%......
是否可以使用javascript强制完整的预加载视频?
答案 0 :(得分:11)
function addSourceToVideo(element, src, type) {
var source = document.createElement('source');
source.src = src;
source.type = type;
element.appendChild(source);
}
var video;
$(document).ready(function(){
video = document.getElementsByTagName('video')[0];
addSourceToVideo( video, "http://your-server.com/clip.ogv", "video/ogv");
addSourceToVideo( video, "http://your-server.com/clip.mp4", "video/mp4");
video.addEventListener("progress", progressHandler,false);
});
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++;
}
}