const synth = window.speechSynthesis; const speak = (text) => { if (text !== "") { // 获得说话文本 const speakText = new SpeechSynthesisUtterance(text); // 验证是否在说话 if (synth.speaking) { console.error("正在说话中..."); return; } // 说话结束触发 speakText.onend = e => { console.log("说话结束"); }; // 发生阻止说出话语的错误就触发 speakText.onerror = e => { console.error("抱歉出错了"); }; speakText.lang = "zh-CN"; // 设置音速和音调 speakText.rate = 0.8; speakText.pitch = 1; // speak synth.speak(speakText); } };