Chrome中Web Speech API的SpeechSynthesis onend事件未触发问题求助
Chrome 113中SpeechSynthesis的onend事件在特定长文本+Google语音场景下未触发
在MacOS的Chrome 113.0.5672.63版本中,使用Web Speech API的SpeechSynthesis组件时遇到了onend事件未触发的问题。Stack Overflow上的旧讨论帖方案已经过时,所以来寻求新的解决办法。
问题细节:
- 仅在使用特定Google高质量语音时出现问题,比如146号Google UK English Male、145号Google US English;0号Samantha语音无此异常。
- 当文本对应的音频时长较长时,
onend事件完全不触发,必须重启Chrome才能暂时恢复;但缩短文本内容或提高语速后,事件可以正常触发。 - 已尝试将utterance对象全局存储(曾经的通用workaround),但无效;在非iframe环境运行代码,问题仍然存在。
复现代码:
var wordsToSpeak = [ 'We propose additional funds for other engine development and for unmanned explorations—explorations which are particularly important for one purpose which this nation will never overlook: the survival of the man who first makes this daring flight.', 'Now end event is fired!', 'You can use global variables to store utterance object.' ]; let voice; const getVoices = () => { if (window.speechSynthesis) { const tmp = speechSynthesis.getVoices(); if (tmp.length !== 0) { // This voice has a problem. voice = tmp[146]; // This voice doesn't have a problem. //voice = tmp[0]; } } }; // this object for storing utterances globally window.utterances = []; getVoices(); speechSynthesis.addEventListener('voiceschanged', getVoices); function speak(text) { var utterance = new SpeechSynthesisUtterance(text); console.log(voice); utterance.voice = voice; utterance.onend = function() { // this code is not called console.log(text); }; // if you will comment this line, end event will not work! window.utterances.push(utterance); speechSynthesis.speak(utterance); } document.getElementById("x").addEventListener("click", () => { for (var i = 0; i < wordsToSpeak.length; i++) { speak(wordsToSpeak[i]) } });
内容的提问来源于stack exchange,提问作者opike
相关产品推荐
相关产品推荐

