You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 09:37:25