Repair the error 'Error: Error generating speech: Failed to execute 'endOfStream' on 'MediaSource': The 'updating' attribute is true on one or more of this MediaSource's SourceBuffers.'

This commit is contained in:
Anthony 2025-03-05 17:04:53 +08:00
parent d67570ab21
commit f4970a92f4

View file

@ -264,7 +264,7 @@ export class AudioService {
// Don't process if audio is in error state // Don't process if audio is in error state
if (this.audio.error) { if (this.audio.error) {
console.warn('Skipping operation due to audio error'); console.warn("Skipping operation due to audio error");
return; return;
} }
@ -272,11 +272,38 @@ export class AudioService {
try { try {
this.sourceBuffer.appendBuffer(operation.chunk); this.sourceBuffer.appendBuffer(operation.chunk);
// Set up event listeners
const onUpdateEnd = () => {
operation.resolve(); operation.resolve();
this.sourceBuffer.removeEventListener("updateend", onUpdateEnd);
this.sourceBuffer.removeEventListener(
"updateerror",
onUpdateError
);
// Process the next operation
this.processNextOperation();
};
const onUpdateError = (event) => {
operation.reject(event);
this.sourceBuffer.removeEventListener("updateend", onUpdateEnd);
this.sourceBuffer.removeEventListener(
"updateerror",
onUpdateError
);
// Decide whether to continue processing
if (event.name !== "InvalidStateError") {
this.processNextOperation();
}
};
this.sourceBuffer.addEventListener("updateend", onUpdateEnd);
this.sourceBuffer.addEventListener("updateerror", onUpdateError);
} catch (error) { } catch (error) {
operation.reject(error); operation.reject(error);
// Only continue processing if it's not a fatal error // Only continue processing if it's not a fatal error
if (error.name !== 'InvalidStateError') { if (error.name !== "InvalidStateError") {
this.processNextOperation(); this.processNextOperation();
} }
} }
@ -367,11 +394,11 @@ export class AudioService {
if (this.audio) { if (this.audio) {
this.audio.pause(); this.audio.pause();
this.audio.src = ''; this.audio.src = "";
this.audio = null; this.audio = null;
} }
if (this.mediaSource && this.mediaSource.readyState === 'open') { if (this.mediaSource && this.mediaSource.readyState === "open") {
try { try {
this.mediaSource.endOfStream(); this.mediaSource.endOfStream();
} catch (e) { } catch (e) {
@ -380,7 +407,11 @@ export class AudioService {
} }
this.mediaSource = null; this.mediaSource = null;
if (this.sourceBuffer) {
this.sourceBuffer.removeEventListener("updateend", () => {});
this.sourceBuffer.removeEventListener("updateerror", () => {});
this.sourceBuffer = null; this.sourceBuffer = null;
}
this.serverDownloadPath = null; this.serverDownloadPath = null;
this.pendingOperations = []; this.pendingOperations = [];
} }
@ -388,17 +419,17 @@ export class AudioService {
cleanup() { cleanup() {
if (this.audio) { if (this.audio) {
this.eventListeners.forEach((listeners, event) => { this.eventListeners.forEach((listeners, event) => {
listeners.forEach(callback => { listeners.forEach((callback) => {
this.audio.removeEventListener(event, callback); this.audio.removeEventListener(event, callback);
}); });
}); });
this.audio.pause(); this.audio.pause();
this.audio.src = ''; this.audio.src = "";
this.audio = null; this.audio = null;
} }
if (this.mediaSource && this.mediaSource.readyState === 'open') { if (this.mediaSource && this.mediaSource.readyState === "open") {
try { try {
this.mediaSource.endOfStream(); this.mediaSource.endOfStream();
} catch (e) { } catch (e) {
@ -407,7 +438,11 @@ export class AudioService {
} }
this.mediaSource = null; this.mediaSource = null;
if (this.sourceBuffer) {
this.sourceBuffer.removeEventListener("updateend", () => {});
this.sourceBuffer.removeEventListener("updateerror", () => {});
this.sourceBuffer = null; this.sourceBuffer = null;
}
this.serverDownloadPath = null; this.serverDownloadPath = null;
this.pendingOperations = []; this.pendingOperations = [];
} }