【发布时间】:2022-09-27 22:54:18
【问题描述】:
我在 SpeechSynthesisUtterance 说话时显示动画图形。我正在使用 onend 属性来检测说话何时完成以删除动画图形。然而,在说话结束和 onend 事件触发之间存在显着延迟,这意味着即使说话已经结束,动画图形也会继续播放大约 1 秒。是否可以消除这种延迟?下面的代码是问题的简单演示。
let utterance = new SpeechSynthesisUtterance(\"Approximately a second delay after utterance has finished and the onend event firing\");
speechSynthesis.speak(utterance);
utterance.onend = function () {
console.log(\"There is a delay before this message appears?\");
}
-
您可以使用
boundary事件并检查事件上的charIndex。如果它位于话语中倒数第二个单词的位置,请更新您的图形,而不是onend。 -
@morganney - 非常感谢您的评论。我已经尝试过您的解决方案,并且效果很好:)
-
太好了,我添加了一个演示示例方法的答案。如果它适合您,请接受它作为答案。
标签: javascript api speech speech-synthesis