我正在尝试通过JavaScript调用SpeechSynthesisUtterance(),以使其在Android Chrome下工作(因为我已阅读此特定功能与Android Chrome兼容)。因此,我编写了一个简短的演示,但不知道如何将其与Android Chrome链接。我调用了窗口函数,但是它不起作用。如果此问题不合适,请通知我,以便将其删除。
<button id="ButtonPlay">TTS demo</button>
<script>
function WhatToPlay(text) {
var msg = new SpeechSynthesisUtterance();
msg.text = text;
window.speechSynthesis.speak(msg);
}
$("#ButtonPlay").click(function() {
WhatToPlay("Hello world");
});
</script>
我在Android Chrome上测试了此基本代码,因此它可以正常工作,因此,如果不在您的设备上,就不会出现其他错误,例如设备本身。
<!DOCTYPE html>
<button type="button" id="speakBtn">Speak</button>
<script>
const speak = () => {
const utter = new SpeechSynthesisUtterance();
utter.text = "Hello";
speechSynthesis.cancel();
speechSynthesis.speak(utter);
}
speakBtn.addEventListener('click', speak);
speechSynthesis.getVoices();
</script>