function initSpeech() { if (!window.speechSynthesis) return; var voices = window.speechSynthesis.getVoices(); if (voices.length > 0) return; window.speechSynthesis.onvoiceschanged = function() {}; } function speakText(text, lang) { if (!text) return; if (!window.speechSynthesis) { return; } if (window.speechSynthesis.getVoices().length === 0) { window.speechSynthesis.onvoiceschanged = function() { doSpeak(text, lang); window.speechSynthesis.onvoiceschanged = null; }; window.speechSynthesis.getVoices(); return; } doSpeak(text, lang); } function doSpeak(text, lang) { window.speechSynthesis.cancel(); var utterance = new SpeechSynthesisUtterance(text); var langMap = { 'es': 'es-ES', 'en': 'en-US', 'zh': 'zh-CN' }; utterance.lang = langMap[lang] || lang; utterance.rate = 0.9; utterance.pitch = 1; window.speechSynthesis.speak(utterance); } if (document.readyState === 'complete') initSpeech(); else document.addEventListener('DOMContentLoaded', initSpeech);