Fix progress

This commit is contained in:
the1812 2018-11-25 20:15:46 +08:00
parent d0e1aac539
commit a7c3845821
4 changed files with 15 additions and 13 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -81,7 +81,7 @@
this.loaded = 0; this.loaded = 0;
this.totalSize = null; this.totalSize = null;
this.workingXhr = null; this.workingXhr = null;
this.fragmentSplitFactor = 10; this.fragmentSplitFactor = 8;
} }
fetchVideoInfo() fetchVideoInfo()
{ {
@ -123,24 +123,27 @@
downloadFragment(fragment) downloadFragment(fragment)
{ {
const promises = []; const promises = [];
const partialLength = fragment.length / this.fragmentSplitFactor; const partialLength = Math.round(fragment.size / this.fragmentSplitFactor);
let startByte = 0; let startByte = 0;
while (startByte <= fragment.length) while (startByte <= fragment.size)
{ {
const range = `bytes=${startByte}-${Math.min(fragment.length - 1, startByte + partialLength)}`; const range = `bytes=${startByte}-${Math.min(fragment.size - 1, Math.round(startByte + partialLength))}`;
promises.push(new Promise((resolve, reject) => promises.push(new Promise((resolve, reject) =>
{ {
let loaded = 0;
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("GET", fragment.url); xhr.open("GET", fragment.url);
xhr.responseType = "arraybuffer"; xhr.responseType = "arraybuffer";
xhr.withCredentials = false; xhr.withCredentials = false;
xhr.addEventListener("progress", (e) => xhr.addEventListener("progress", (e) =>
{ {
this.progress && this.progress((this.loaded + e.loaded) / this.totalSize); this.loaded += e.loaded - loaded;
loaded = e.loaded;
this.progress && this.progress(this.loaded / this.totalSize);
}); });
xhr.addEventListener("load", () => xhr.addEventListener("load", () =>
{ {
if (xhr.status === 200) if (("" + xhr.status)[0] === "2")
{ {
resolve(xhr.response); resolve(xhr.response);
} }
@ -154,7 +157,7 @@
xhr.setRequestHeader("Range", range); xhr.setRequestHeader("Range", range);
xhr.send(); xhr.send();
})); }));
startByte += partialLength; startByte = Math.round(startByte + partialLength);
} }
this.workingXhr = Promise.all(promises); this.workingXhr = Promise.all(promises);
return this.workingXhr; return this.workingXhr;
@ -223,7 +226,6 @@
for (const fragment of this.fragments) for (const fragment of this.fragments)
{ {
const data = await this.downloadFragment(fragment); const data = await this.downloadFragment(fragment);
this.loaded += fragment.size;
downloadedData.push(data); downloadedData.push(data);
} }
if (downloadedData.length < 1) if (downloadedData.length < 1)