Skip to content

Commit fd2595d

Browse files
committed
in RemoteTtsEngine use separate fetch step to capture meaningful error messages because Audio element (on FF) only throws the generic "Failed to open media" error
1 parent 28a7313 commit fd2595d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

js/tts-engines.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,16 @@ function RemoteTtsEngine(serviceUrl) {
189189
audio.defaultPlaybackRate = options.rate;
190190
}
191191
speakPromise = ready(options)
192-
.then(function() {
193-
audio.src = getAudioUrl(utterance, options.lang, options.voice);
192+
.then(async function() {
193+
const url = getAudioUrl(utterance, options.lang, options.voice)
194+
const res = await fetch(url)
195+
if (!res.ok) {
196+
const msg = await res.text().catch(err => "")
197+
throw new Error(msg || (res.status + " " + res.statusText))
198+
}
199+
const blob = await res.blob()
200+
if (audio.src) URL.revokeObjectURL(audio.src)
201+
audio.src = URL.createObjectURL(blob)
194202
return new Promise(function(fulfill) {audio.oncanplay = fulfill});
195203
})
196204
.then(function() {
@@ -235,7 +243,8 @@ function RemoteTtsEngine(serviceUrl) {
235243
}
236244
this.prefetch = function(utterance, options) {
237245
if (!iOS) {
238-
ajaxGet(getAudioUrl(utterance, options.lang, options.voice, true));
246+
fetch(getAudioUrl(utterance, options.lang, options.voice, true))
247+
.catch(console.error)
239248
}
240249
}
241250
this.setNextStartTime = function(time, options) {

0 commit comments

Comments
 (0)