Add abort controller to TTS

This commit is contained in:
dwrz
2026-07-08 18:31:12 +00:00
parent 0e768eca84
commit 90f7439161

View File

@@ -24,6 +24,7 @@ export class TTSPlayer extends EventTarget {
this.currentAudio = null; this.currentAudio = null;
this.currentAudioURL = null; this.currentAudioURL = null;
this._muted = false; this._muted = false;
this._abortController = null;
} }
/** /**
@@ -46,6 +47,10 @@ export class TTSPlayer extends EventTarget {
*/ */
stop() { stop() {
this._stopCurrentAudio(); this._stopCurrentAudio();
if (this._abortController) {
this._abortController.abort();
this._abortController = null;
}
this.playQueue = []; this.playQueue = [];
this._playing = false; this._playing = false;
@@ -124,6 +129,9 @@ export class TTSPlayer extends EventTarget {
* @private * @private
*/ */
async _synthesizeAndPlay(text, voice) { async _synthesizeAndPlay(text, voice) {
this._abortController = new AbortController();
const { signal } = this._abortController;
try { try {
this._stopCurrentAudio(); this._stopCurrentAudio();
@@ -136,6 +144,7 @@ export class TTSPlayer extends EventTarget {
voice: voice || this.defaultVoice, voice: voice || this.defaultVoice,
response_format: 'wav', response_format: 'wav',
}), }),
signal,
}); });
if (!res.ok) { if (!res.ok) {
@@ -169,8 +178,11 @@ export class TTSPlayer extends EventTarget {
this.currentAudio.play().catch(reject); this.currentAudio.play().catch(reject);
}); });
} catch (e) { } catch (e) {
if (e.name === 'AbortError') return;
console.error('TTS synthesis failed:', e); console.error('TTS synthesis failed:', e);
this._cleanupAudio(); this._cleanupAudio();
} finally {
this._abortController = null;
} }
} }