Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML5语音合成_Javascript_Html_Text To Speech - Fatal编程技术网

Javascript HTML5语音合成

Javascript HTML5语音合成,javascript,html,text-to-speech,Javascript,Html,Text To Speech,简单地说,它非常简单,包含最少的条件(只有两行) 但我还是什么也没听到。但谷歌TTS在我的笔记本电脑上运行得非常好 当我运行下面的页面时,我只看到“一两”警报 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> var utterance = new SpeechSynthesisUtterance(

简单地说,它非常简单,包含最少的条件(只有两行)

但我还是什么也没听到。但谷歌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>

var话语=新的话语(“你好,宝贝”);
窗口。演讲合成。讲话(话语);
警惕(“一二”);

我使用谷歌浏览器36版。如何查看JavaScript中的错误?非常感谢堆栈溢出

我帮谷歌chrome安装了语音合成器。很高兴你正在使用它

**编辑:我在Chrome36控制台上运行了你的代码,效果很好**

您应该这样使用它:

if('speechSynthesis' in window){
    var speech = new SpeechSynthesisUtterance('hello baby');
    speech.lang = 'en-US';
    window.speechSynthesis.speak(speech);
}
您可以在chrome中的控制台中检查错误,方法是在页面上单击鼠标右键,然后在上下文菜单中单击最后一个选项(inspect元素)


更多信息:

以上答案很好,但如果您想选择其他声音,请使用以下选项:

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.");
函数说某事(whatToSay){
const synth=window.speechSynthesis;
//在此处输入您的语音,因为语音列表是异步加载的。请检查下面的console.log。
getVoice(“谷歌美国英语”,synth)
.然后(语音=>{
var utterThis=新演讲(whatToSay);
发出这个声音;
合成说话(说这个);
})
.catch(error=>console.log(“error:,error));
}
函数getVoice(voiceName,synth){
返回新承诺((解决、拒绝)=>{
synth.onvoiceschanged=函数(){
const voices=synth.getVoices();
log(“查看系统上所有可用的语言和语音:”,voices);
for(设i=0;i
刚意识到我们的代码是一样的。你的代码在Chrome36中对我有效。你听到“你好”这个词了吗?我听到了整个事情。非常感谢你的帮助。我目前正在开发一个移动应用程序,可以帮助非英语国家的学生学习英语,如韩国、德国、日本、中国、法国。你推荐谷歌TTS还是HTML5 TTS?如果我选择使用谷歌TTS,我是否必须在我的应用程序中的某个地方给谷歌积分?如果这是必要的,那么我没有任何问题给予谷歌信用。