Simplify createAudio function (#1728)

This commit is contained in:
toasted-nutbread 2021-06-04 20:17:04 -04:00 committed by GitHub
parent c7e6c37095
commit 57fb496fbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,18 +42,13 @@ class AudioSystem extends EventDispatcher {
return this._fallbackAudio;
}
createAudio(url, sourceType) {
return new Promise((resolve, reject) => {
const audio = new Audio(url);
audio.addEventListener('loadeddata', () => {
if (!this._isAudioValid(audio, sourceType)) {
reject(new Error('Could not retrieve audio'));
} else {
resolve(audio);
}
});
audio.addEventListener('error', () => reject(audio.error));
});
async createAudio(url, sourceType) {
const audio = new Audio(url);
await this._waitForData(audio);
if (!this._isAudioValid(audio, sourceType)) {
throw new Error('Could not retrieve audio');
}
return audio;
}
createTextToSpeechAudio(text, voiceUri) {
@ -70,6 +65,13 @@ class AudioSystem extends EventDispatcher {
this.trigger('voiceschanged', e);
}
_waitForData(audio) {
return new Promise((resolve, reject) => {
audio.addEventListener('loadeddata', () => resolve());
audio.addEventListener('error', () => reject(audio.error));
});
}
_isAudioValid(audio, sourceType) {
switch (sourceType) {
case 'jpod101':