fix: TTS cucina - gestione asincrona voci e delay dopo cancel()
This commit is contained in:
+19
-7
@@ -6076,14 +6076,26 @@ function renderCookingStep() {
|
|||||||
function speakCookingStep(text) {
|
function speakCookingStep(text) {
|
||||||
if (!('speechSynthesis' in window)) return;
|
if (!('speechSynthesis' in window)) return;
|
||||||
window.speechSynthesis.cancel();
|
window.speechSynthesis.cancel();
|
||||||
const utt = new SpeechSynthesisUtterance(text);
|
|
||||||
utt.lang = 'it-IT';
|
function _doSpeak() {
|
||||||
utt.rate = 0.9;
|
const utt = new SpeechSynthesisUtterance(text);
|
||||||
utt.pitch = 1.0;
|
utt.lang = 'it-IT';
|
||||||
|
utt.rate = 0.9;
|
||||||
|
utt.pitch = 1.0;
|
||||||
|
const voices = window.speechSynthesis.getVoices();
|
||||||
|
const itVoice = voices.find(v => v.lang.startsWith('it'));
|
||||||
|
if (itVoice) utt.voice = itVoice;
|
||||||
|
window.speechSynthesis.speak(utt);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Voices may not be ready yet on first load — wait for voiceschanged if empty
|
||||||
const voices = window.speechSynthesis.getVoices();
|
const voices = window.speechSynthesis.getVoices();
|
||||||
const itVoice = voices.find(v => v.lang.startsWith('it'));
|
if (voices.length > 0) {
|
||||||
if (itVoice) utt.voice = itVoice;
|
// Small delay after cancel() to avoid the speak being silently dropped
|
||||||
window.speechSynthesis.speak(utt);
|
setTimeout(_doSpeak, 50);
|
||||||
|
} else {
|
||||||
|
window.speechSynthesis.addEventListener('voiceschanged', () => setTimeout(_doSpeak, 50), { once: true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleCookingTTS() {
|
function toggleCookingTTS() {
|
||||||
|
|||||||
Reference in New Issue
Block a user