Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch API #246

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/track/loader/FetchLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Loader from "./Loader";

export default class extends Loader {
/**
* Loads an audio file via fetch API.
*/
load() {
return new Promise((resolve, reject) => {
fetch(this.src)
.then((data) => data.arrayBuffer())
.then((arrayBuffer) => super.fetchLoad(arrayBuffer))
.then((decodedAudio) => resolve(decodedAudio))
.catch((err) => {
reject(Error(`Track ${this.src} failed to load with error: ${err}`));
});
});
}
}
24 changes: 24 additions & 0 deletions src/track/loader/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,28 @@ export default class {
);
});
}

fetchLoad(arrayBuffer) {
this.setStateChange(STATE_DECODING);

return new Promise((resolve, reject) => {
this.ac.decodeAudioData(
arrayBuffer,
(audioBuffer) => {
this.audioBuffer = audioBuffer;
this.setStateChange(STATE_FINISHED);

resolve(audioBuffer);
},
(err) => {
if (err === null) {
// Safari issues with null error
reject(Error("MediaDecodeAudioDataUnknownContentType"));
} else {
reject(err);
}
}
);
});
}
}
4 changes: 3 additions & 1 deletion src/track/loader/LoaderFactory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BlobLoader from "./BlobLoader";
import IdentityLoader from "./IdentityLoader";
import XHRLoader from "./XHRLoader";
import FetchLoader from "./FetchLoader";

export default class {
static createLoader(src, audioContext, ee) {
Expand All @@ -9,7 +10,8 @@ export default class {
} else if (src instanceof AudioBuffer) {
return new IdentityLoader(src, audioContext, ee);
} else if (typeof src === "string") {
return new XHRLoader(src, audioContext, ee);
//return new XHRLoader(src, audioContext, ee);
return new FetchLoader(src, audioContext, ee);
}

throw new Error("Unsupported src type");
Expand Down