Cec*_*uez 5 javascript webspeech-api
所以我使用修改后的脚本尝试从Web Speech API播放一些文本.
代码最初在这里:
这是我修改后的变体:
function googleSpeech(text, rate) {
if (!reading) {
speechSynthesis.cancel();
if (timer) {
clearInterval(timer);
}
let msg = new SpeechSynthesisUtterance();
let voices = window.speechSynthesis.getVoices();
msg.voice = voices[63];
msg.voiceURI = 'native';
msg.volume = 1; // 0 to 1
msg.rate = rate; // 0.1 to 10
msg.pitch = 1; //0 to 2
msg.text = text;
msg.lang = 'zh-CN';
msg.onerror = function (e) {
speechSynthesis.cancel();
reading = false;
clearInterval(timer);
};
msg.onpause = function (e) {
};
msg.onboundary = function (event) {
};
msg.onend = function (e) {
speechSynthesis.cancel();
reading = false;
clearInterval(timer);
};
speechSynthesis.onerror = function (e) {
speechSynthesis.cancel();
reading = false;
clearInterval(timer);
};
console.log(msg);
speechSynthesis.speak(msg);
timer = setInterval(function () {
if (speechSynthesis.paused) {
speechSynthesis.resume();
}
}, 100);
reading = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以让它玩ONCE.每当我试图让它再次播放时,它都不起作用 - 这是尽管跑步speechSynthesis.cancel();.当我重新加载页面时,一切正常 - 一次播放.
是否有任何一致的方式再次播放文本?看起来这可能与Web Speech API中的许多错误有关.这是在Chrome 68上.
以下是我正在播放的文字示例:
???????????? ????????? ???????
Run Code Online (Sandbox Code Playgroud)
答案实际上与利率财产的价值有关。
虽然 Web Speech API 的官方文档说它支持 0.1 到 10 的速率值,但 Chrome(至少是 Chrome 68,我正在运行的版本)似乎并不完全支持超出范围的速率.5至2。
任何超出此范围的内容都会在使用一次 API 后导致声音分散和中断,并且在页面刷新之前声音将不再起作用。