我在 Google Chrome 上尝试了以下操作。它会打印出来0
声音,然后再次打印0
我的电脑上有声音,但如果我重新加载页面以重新开始,并将最后一行更改为setTimeout(foo, 1000)
,那么第一行会打印出来0
和第二行,87
.
该数字甚至可以更改为66
or 33
它有效,但我不确定它是否取决于计算机速度和网络速度(用于获取语音数据。(可能是内部网络流量,因为我在调试器的网络选项卡中没有看到任何流量)) 。
有没有更可靠的方法获得ready
类似于 DOMReady 事件的事件或调用回调的某种方式,以便它更可靠? (而不是使用setTimeout
推迟一段未知的时间并希望得到最好的结果)。
function foo() {
msg = new SpeechSynthesisUtterance(),
voices = window.speechSynthesis.getVoices();
console.log("How many voices", voices.length);
msg.text = "Hello World";
speechSynthesis.speak(msg);
}
foo();
setTimeout(foo, 0); // just try again in the next event cycle
The voiceschanged event https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/voiceschanged_event可能会有所帮助:
The voiceschanged
当以下列表出现时,将触发 Web Speech API 的事件SpeechSynthesisVoice
将返回的对象SpeechSynthesis.getVoices()
方法已更改(当voiceschanged
事件触发。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)