简单地说,它非常简单,并且包含最少的conding (只有两行)
但我还是什么也没听到。但是Google TTS在我的笔记本电脑上运行得很好。
当我运行下面的页面时,我只看到“一二”警告。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head>
<body>
<script>
var utterance = new SpeechSynthesisUtterance('Hello baby');
window.speechSynthesis.speak(utterance);
alert("one two");
</script>
</body>
</html>
我使用的是谷歌Chrome 36版。如何查看JavaScript中的错误?非常感谢Stack Overflow!
发布于 2014-08-16 10:35:24
我帮着把speech synth放到了google chrome中。很高兴你正在使用它!
**编辑:我在Chrome 36控制台上运行了你的代码,运行正常**
你应该这样使用它:
if('speechSynthesis' in window){
var speech = new SpeechSynthesisUtterance('hello baby');
speech.lang = 'en-US';
window.speechSynthesis.speak(speech);
}
你可以通过右键点击页面和上下文菜单中的最后一个选项( inspect element ),在chrome中的控制台中检查错误。
更多信息请点击这里:https://developer.chrome.com/devtools/docs/console
发布于 2017-12-05 23:01:29
上面的答案效果很好,但如果你想选择另一个声音,请使用以下代码:
function saySomething(whatToSay){
const synth = window.speechSynthesis;
// enter your voice here, because voices list loads asynchronously.. check the console.log below.
getVoice("Google US English", synth)
.then(voice => {
var utterThis = new SpeechSynthesisUtterance(whatToSay);
utterThis.voice = voice;
synth.speak(utterThis);
})
.catch(error => console.log("error: ", error));
}
function getVoice(voiceName, synth){
return new Promise((resolve, reject) =>{
synth.onvoiceschanged = function(){
const voices = synth.getVoices();
console.log("see all available languages and voices on your system: ", voices);
for(let i = 0; i < voices.length ; i++) {
if(voices[i].name == voiceName) {
resolve(voices[i]);
}
}
}
synth.getVoices();
});
}
saySomething("Mu ha ha! Works now.");
https://stackoverflow.com/questions/25336428
复制相似问题