Skip to content

Commit a578d22

Browse files
Merge pull request #221 from Chuui9739/fix-MediaSource-error
Repair the error 'Error: Error generating speech: Failed to execute '…
2 parents d67570a + d69a4c3 commit a578d22

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

web/src/services/AudioService.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,46 @@ export class AudioService {
264264

265265
// Don't process if audio is in error state
266266
if (this.audio.error) {
267-
console.warn('Skipping operation due to audio error');
267+
console.warn("Skipping operation due to audio error");
268268
return;
269269
}
270270

271271
const operation = this.pendingOperations.shift();
272-
272+
273273
try {
274274
this.sourceBuffer.appendBuffer(operation.chunk);
275-
operation.resolve();
275+
276+
// Set up event listeners
277+
const onUpdateEnd = () => {
278+
operation.resolve();
279+
this.sourceBuffer.removeEventListener("updateend", onUpdateEnd);
280+
this.sourceBuffer.removeEventListener(
281+
"updateerror",
282+
onUpdateError
283+
);
284+
// Process the next operation
285+
this.processNextOperation();
286+
};
287+
288+
const onUpdateError = (event) => {
289+
operation.reject(event);
290+
this.sourceBuffer.removeEventListener("updateend", onUpdateEnd);
291+
this.sourceBuffer.removeEventListener(
292+
"updateerror",
293+
onUpdateError
294+
);
295+
// Decide whether to continue processing
296+
if (event.name !== "InvalidStateError") {
297+
this.processNextOperation();
298+
}
299+
};
300+
301+
this.sourceBuffer.addEventListener("updateend", onUpdateEnd);
302+
this.sourceBuffer.addEventListener("updateerror", onUpdateError);
276303
} catch (error) {
277304
operation.reject(error);
278305
// Only continue processing if it's not a fatal error
279-
if (error.name !== 'InvalidStateError') {
306+
if (error.name !== "InvalidStateError") {
280307
this.processNextOperation();
281308
}
282309
}
@@ -364,14 +391,14 @@ export class AudioService {
364391
this.controller.abort();
365392
this.controller = null;
366393
}
367-
394+
368395
if (this.audio) {
369396
this.audio.pause();
370-
this.audio.src = '';
397+
this.audio.src = "";
371398
this.audio = null;
372399
}
373400

374-
if (this.mediaSource && this.mediaSource.readyState === 'open') {
401+
if (this.mediaSource && this.mediaSource.readyState === "open") {
375402
try {
376403
this.mediaSource.endOfStream();
377404
} catch (e) {
@@ -380,25 +407,29 @@ export class AudioService {
380407
}
381408

382409
this.mediaSource = null;
383-
this.sourceBuffer = null;
410+
if (this.sourceBuffer) {
411+
this.sourceBuffer.removeEventListener("updateend", () => {});
412+
this.sourceBuffer.removeEventListener("updateerror", () => {});
413+
this.sourceBuffer = null;
414+
}
384415
this.serverDownloadPath = null;
385416
this.pendingOperations = [];
386417
}
387418

388419
cleanup() {
389420
if (this.audio) {
390421
this.eventListeners.forEach((listeners, event) => {
391-
listeners.forEach(callback => {
422+
listeners.forEach((callback) => {
392423
this.audio.removeEventListener(event, callback);
393424
});
394425
});
395-
426+
396427
this.audio.pause();
397-
this.audio.src = '';
428+
this.audio.src = "";
398429
this.audio = null;
399430
}
400431

401-
if (this.mediaSource && this.mediaSource.readyState === 'open') {
432+
if (this.mediaSource && this.mediaSource.readyState === "open") {
402433
try {
403434
this.mediaSource.endOfStream();
404435
} catch (e) {
@@ -407,7 +438,11 @@ export class AudioService {
407438
}
408439

409440
this.mediaSource = null;
410-
this.sourceBuffer = null;
441+
if (this.sourceBuffer) {
442+
this.sourceBuffer.removeEventListener("updateend", () => {});
443+
this.sourceBuffer.removeEventListener("updateerror", () => {});
444+
this.sourceBuffer = null;
445+
}
411446
this.serverDownloadPath = null;
412447
this.pendingOperations = [];
413448
}

0 commit comments

Comments
 (0)