gpt4 book ai didi

javascript - SpeechSynthesis API onend 回调不起作用

转载 作者:行者123 更新时间:2023-12-02 22:25:16 26 4
gpt4 key购买 nike

我正在使用Speech Synthesis API在 Google Chrome v34.0.1847.131 上。该 API 从 v33 开始在 Chrome 中实现。

文本转语音在大多数情况下都有效,除非为 onend 分配回调。例如下面的代码:

var message = window.SpeechSynthesisUtterance("Hello world!");
message.onend = function(event) {
console.log('Finished in ' + event.elapsedTime + ' seconds.');
};
window.speechSynthesis.speak(message);

有时会调用onend,有时不会调用它。时机似乎完全不对劲。当它被调用时,打印的 elapsedTime 始终是某个纪元时间,例如 1399237888

最佳答案

根据this comment关于the answer中提到的错误来自 Kevin Hakanson 的说法,这可能是垃圾收集的问题。在调用 speak 之前将话语存储在变量中 seems to do the trick :

window.utterances = [];
var utterance = new SpeechSynthesisUtterance( 'hello' );
utterances.push( utterance );
speechSynthesis.speak( utterance );

关于javascript - SpeechSynthesis API onend 回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23483990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com