使用Javascript强制完整的预加载HTML5视频?

时间:2013-05-16 07:51:16

标签: google-chrome html5-video preload

我即将尝试在HTML5中完全预加载视频。我使用属性preload=auto但它没有预加载整个视频...在Chrome中,视频只预装了2%,Safari大约50%......

是否可以使用javascript强制完整的预加载视频?

1 个答案:

答案 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++;    
    }    
}