diff --git a/assets/js/app.js b/assets/js/app.js index a300f61..16a7e9e 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -6076,14 +6076,26 @@ function renderCookingStep() { function speakCookingStep(text) { if (!('speechSynthesis' in window)) return; window.speechSynthesis.cancel(); - const utt = new SpeechSynthesisUtterance(text); - utt.lang = 'it-IT'; - utt.rate = 0.9; - utt.pitch = 1.0; + + function _doSpeak() { + const utt = new SpeechSynthesisUtterance(text); + 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 itVoice = voices.find(v => v.lang.startsWith('it')); - if (itVoice) utt.voice = itVoice; - window.speechSynthesis.speak(utt); + if (voices.length > 0) { + // Small delay after cancel() to avoid the speak being silently dropped + setTimeout(_doSpeak, 50); + } else { + window.speechSynthesis.addEventListener('voiceschanged', () => setTimeout(_doSpeak, 50), { once: true }); + } } function toggleCookingTTS() {