如果您查看响应式语音代码,他们的代码中有一个奇怪的 ~200 毫秒超时:
a.enableFallbackMode()) : setTimeout(function() {
var b = setInterval(function() {
var c = window.speechSynthesis.getVoices();
0 != c.length || null != a.systemvoices &&
0 != a.systemvoices.length ? (console.log("RV: Voice support ready"),
a.systemVoicesReady(c),
clearInterval(b)) : (console.log("Voice support NOT ready"),
a.voicesupport_attempts++,
a.voicesupport_attempts > a.VOICESUPPORT_ATTEMPTLIMIT && (clearInterval(b),
null != window.speechSynthesis ? a.iOS ? (a.iOS9 ? a.systemVoicesReady(a.cache_ios9_voices) : a.systemVoicesReady(a.cache_ios_voices),
console.log("RV: Voice support ready (cached)")) : (console.log("RV: speechSynthesis present but no system voices found"),
a.enableFallbackMode()) :
a.enableFallbackMode()))
}, 100)
}, 100);
a.Dispatch("OnLoad")
如果您在超时前尝试使用语音,您将点击此控制台日志:
1570RV:错误:未找到适合:美国英语女性的声音
根据我的经验,这很糟糕,应该会引发错误。
如果您想继续使用此脚本,唯一的解决方案是等待至少 201 毫秒以等待所有声音加载(但您只需执行一次)
let readItOnce = false;
function read(){
const readIt = () => {
readItOnce = true;
responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female');
responsiveVoice.speak('Love you.','US English Female');
}
if (!readItOnce) { setTimeout(readIt, 201)}
else { readIt(); }
}
还要执行此处的建议:https : //stackoverflow.com/a/36654326/561731 在函数的 onload 中。
<html>
<head>
<script src="https://code.responsivevoice.org/responsivevoice.js"></script>
<script type="text/javascript">
let readItOnce = false;
function read(){
const readIt = () => {
readItOnce = true;
responsiveVoice.speak('Lily, you can not just freeze me out like this.','US English Female');
responsiveVoice.speak('Love you.','US English Female');
}
if (!readItOnce) { setTimeout(readIt, 201)}
else { readIt(); }
}
</script>
</head>
<body onload="read();">
</body>
</html>