Simplify createAudio function (#1728)
This commit is contained in:
parent
c7e6c37095
commit
57fb496fbc
@ -42,18 +42,13 @@ class AudioSystem extends EventDispatcher {
|
|||||||
return this._fallbackAudio;
|
return this._fallbackAudio;
|
||||||
}
|
}
|
||||||
|
|
||||||
createAudio(url, sourceType) {
|
async createAudio(url, sourceType) {
|
||||||
return new Promise((resolve, reject) => {
|
const audio = new Audio(url);
|
||||||
const audio = new Audio(url);
|
await this._waitForData(audio);
|
||||||
audio.addEventListener('loadeddata', () => {
|
if (!this._isAudioValid(audio, sourceType)) {
|
||||||
if (!this._isAudioValid(audio, sourceType)) {
|
throw new Error('Could not retrieve audio');
|
||||||
reject(new Error('Could not retrieve audio'));
|
}
|
||||||
} else {
|
return audio;
|
||||||
resolve(audio);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
audio.addEventListener('error', () => reject(audio.error));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createTextToSpeechAudio(text, voiceUri) {
|
createTextToSpeechAudio(text, voiceUri) {
|
||||||
@ -70,6 +65,13 @@ class AudioSystem extends EventDispatcher {
|
|||||||
this.trigger('voiceschanged', e);
|
this.trigger('voiceschanged', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_waitForData(audio) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
audio.addEventListener('loadeddata', () => resolve());
|
||||||
|
audio.addEventListener('error', () => reject(audio.error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
_isAudioValid(audio, sourceType) {
|
_isAudioValid(audio, sourceType) {
|
||||||
switch (sourceType) {
|
switch (sourceType) {
|
||||||
case 'jpod101':
|
case 'jpod101':
|
||||||
|
Loading…
Reference in New Issue
Block a user