在谷歌中缓存下载视频离线观看,js代码

 var download=function(urlInfo)
                {
                    when(createFile(localFileName))
                    .then(function (fileInfo)
                    {
                        var downloaded = function (downloadInfo)
                        {
                            vueThis.cacheStatus = "downloaded";
                            vueThis.downloadProgress = 100;
                            weui.toast("Cache is done");
                            fileInfo.fileWriter.onwriteend = function ()
                            {
                                var mainPlayer = document.getElementById("mainPlayer");
                                var prevTime = mainPlayer.currentTime;
                                mainPlayer.src = fileInfo.fileEntry.toURL();
                                mainPlayer.currentTime = prevTime;
                                mainPlayer.play();                       
                                vueThis.cacheStatus = "playOffline";
                                vueThis.downloadProgress = 100;
                                weui.toast("Offline playing");
                            };
                            fileInfo.fileWriter.write(downloadInfo.blob);
                        };

                        vueThis.cacheStatus = "downloading";
                        vueThis.cacheMessage = 'Total ' + (urlInfo.size / (1024 * 2014)).toFixed(2) + 'MB';
                        var downloader = Downloader(vueThis.mediaUrl, urlInfo.mimeType, downloadOnProgress);
                        when(downloader).then(downloaded).otherwise(function (e) { console.log(e) });
                    });
                };

  

function Downloader(url, mimeType, onProgressCallback)
{
var deferred = when.defer();
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
//if url is from other domains,
//Cors settings of that domain is required
//the settings is as following
//Access-Control-Allow-Origin *.youzack.com;
//Access - Control - Allow - Methods GET;
//Access - Control - Expose - Headers Content - Type, Content - Length;
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.response != null) {
var size = parseInt(this.response.byteLength);
var blob = new Blob([new Uint8Array(this.response)], {type: mimeType});
var ret = {'size' : size,'blob' : blob};
deferred.resolve(ret);
}
}
};
xhr.onprogress = function(e) {
if(onProgressCallback)
{
onProgressCallback(e);
}
};
xhr.responseType = 'arraybuffer';
xhr.send();
return deferred.promise;
};

猜你喜欢

转载自www.cnblogs.com/Tom-yi/p/11738633.html