Skip to content

Commit f456167

Browse files
committed
fix tts on first load
1 parent 3cbacf5 commit f456167

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/agent.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,23 @@ export default class Agent {
633633
utterance.rate = this._tts.rate;
634634
utterance.pitch = this._tts.pitch;
635635
const voices = speechSynthesis.getVoices();
636-
const match = voices.find((v) => v.name.includes(this._tts!.voice));
637-
if (match) utterance.voice = match;
638-
speechSynthesis.speak(utterance);
636+
if (voices.length > 0) {
637+
const match = voices.find((v) => v.name.includes(this._tts!.voice));
638+
if (match) utterance.voice = match;
639+
speechSynthesis.speak(utterance);
640+
} else {
641+
// Voices not loaded yet (first page load) — wait for them
642+
speechSynthesis.addEventListener(
643+
"voiceschanged",
644+
() => {
645+
const v = speechSynthesis.getVoices();
646+
const match = v.find((voice) => voice.name.includes(this._tts!.voice));
647+
if (match) utterance.voice = match;
648+
speechSynthesis.speak(utterance);
649+
},
650+
{ once: true },
651+
);
652+
}
639653
}
640654

641655
dispose() {

0 commit comments

Comments
 (0)