【问题标题】:HTML5 Speech SynthesisHTML5 语音合成
【发布时间】:2014-10-09 18:43:11
【问题描述】:

简单地说,它非常简单,包含最少的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>

我使用的是 Google Chrome 版本 36。如何查看我的 JavaScript 中的错误?非常感谢 Stack Overflow!

【问题讨论】:

  • 刚刚发现代码是一样的。您的代码在 Chrome 36 中适用于我。
  • 你听到“你好”这个词了吗?
  • 我听到了整个事情。

标签: javascript html text-to-speech


【解决方案1】:

我帮助将语音合成器放入 google chrome。很高兴您正在使用它!

**编辑:我在 Chrome 36 控制台中运行了您的代码,它运行良好 **

你应该像这样使用它:

if('speechSynthesis' in window){
    var speech = new SpeechSynthesisUtterance('hello baby');
    speech.lang = 'en-US';
    window.speechSynthesis.speak(speech);
}

您可以通过右键单击页面并在上下文菜单中单击最后一个选项(检查元素)来检查 chrome 控制台中的错误。

更多:https://developer.chrome.com/devtools/docs/console

【讨论】:

  • 非常感谢您的帮助。我目前正在构建一个移动应用程序,可以帮助韩国、德国、日本、中国、法国等非英语国家的学生学习英语。你推荐谷歌 TTS 还是 HTML5 TTS?如果我选择使用 Google TTS,我是否必须在我的应用程序中的某个地方向 Google 提供信用?如果需要,那么我对授予 Google 信用没有任何问题。
【解决方案2】:

上面的答案效果很好,但如果你想选择另一个声音,请使用:

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.");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多