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