aboutsummaryrefslogtreecommitdiff
path: root/ext/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-06-04 20:17:04 -0400
committerGitHub <noreply@github.com>2021-06-04 20:17:04 -0400
commit57fb496fbcd2c31a462c2090e2391594c1ca8215 (patch)
tree333566adc49c09ffc8489398d1775c7e69b71d77 /ext/js
parentc7e6c370953608bb5ff18143e9e80defdc09ad88 (diff)
Simplify createAudio function (#1728)
Diffstat (limited to 'ext/js')
-rw-r--r--ext/js/media/audio-system.js26
1 files changed, 14 insertions, 12 deletions
diff --git a/ext/js/media/audio-system.js b/ext/js/media/audio-system.js
index 03e68767..c5207341 100644
--- a/ext/js/media/audio-system.js
+++ b/ext/js/media/audio-system.js
@@ -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':