【发布时间】:2017-07-30 09:26:36
【问题描述】:
我正在尝试使用Speechsynthesis 的简单示例。
<script>
voices = window.speechSynthesis.getVoices()
var utterance = new SpeechSynthesisUtterance("Hello World");
utterance.voice = voices[4];
utterance.lang = voices[4].lang;
window.speechSynthesis.speak(utterance);
</script>
但这给出了声音未定义的错误。我发现 getVoices() 是异步加载的。我看到this 的回答并更新了我的代码,如下所示以使用回调。
<script>
window.speechSynthesis.onvoiceschanged = function() {
voices = window.speechSynthesis.getVoices()
var utterance = new SpeechSynthesisUtterance("Hello World");
utterance.voice = voices[4];
utterance.lang = voices[4].lang;
window.speechSynthesis.speak(utterance);
};
</script>
但是由于一些奇怪的原因,文本被说了三遍而不是一遍。如何修复此代码?
【问题讨论】:
标签: javascript text-to-speech speech-synthesis