【问题标题】:How can I get female voice by Web Speech API in Google Chrome如何通过 Google Chrome 中的 Web Speech API 获取女性语音
【发布时间】:2016-10-24 09:50:38
【问题描述】:

在网页中,我想要一个女性声音来朗读我的文字。我试图通过以下代码来做到这一点。但现在仍然是男声在说话。我怎样才能安排一个女性的声音来说话我的文本?谁能分享我在谷歌浏览器中工作的正确代码。

var voices = speechSynthesis.getVoices();
var msg = new SpeechSynthesisUtterance("Hello World!");
msg.default=false; 
msg.localservice=true;
msg.lang = "en-GB";
msg.voice = voices[3].name;
speechSynthesis.speak(msg);

【问题讨论】:

    标签: web speech-synthesis


    【解决方案1】:

    下面的代码对我有用。我希望它对你有用。

    var msg = new SpeechSynthesisUtterance();
    var voices = window.speechSynthesis.getVoices();
    msg.voice = voices[3];
    msg.text = "Hello World";
    speechSynthesis.speak(msg);
    

    第一次尝试它可能会发出男性的声音。但是在第二次尝试(不刷新)时,它会发出女性的声音并尝试将其部署在虚拟服务器上,它会在第一次运行时像魅力一样工作。

    【讨论】:

    • stackoverflow.com/questions/45877555/… ,我遇到了一些奇怪的事情。
    • 这取决于列出的声音。你只是在访问一个硬编码的属性,如果语音列表由于某种原因发生变化,你会得到不同的声音。
    【解决方案2】:

    这发生在我第一次加载页面时声音没有改变。

    我找到了适合我的解决方案。
    getVoices() 应该在文档准备就绪时触发。
    所以我像这样在我的js顶部添加。

       $(document).ready(function() {
            var voices = window.speechSynthesis.getVoices();
        })
    

    【讨论】:

      【解决方案3】:

      在 Chrome 上,我会这样做:

      <html>
      <head>
      <script>
          function speak(language, country, preferredNames, text) {
              var resolvePromise;
              var promise = new Promise(r => resolvePromise = r);
      
              var voices = window.speechSynthesis.getVoices();
              if (voices.length == 0) {
                  new Promise(r => {
                      window.speechSynthesis.onvoiceschanged = () => { 
                              window.speechSynthesis.onvoiceschanged = null;
                              r(); 
                          };
                  })
                  .then(() => speak(language, country, preferredNames, text))
                  .then(resolvePromise);
              } else {
                  var msg = new SpeechSynthesisUtterance(text);
                  voices.sort((a, b) => {
                      if (language != null) {
                          var matchA = a.lang.indexOf(language + "-") == 0;
                          var matchB = b.lang.indexOf(language + "-") == 0;
                          if (! matchA && ! matchB) return 0;
                          if (! matchA && matchB) return 1;
                          if (! matchB && matchA) return -1;
                      }
                      if (country != null) {
                          var matchA = a.lang.indexOf("-" + country) == a.lang.length - ("-" + country).length;
                          var matchB = b.lang.indexOf("-" + country) == b.lang.length - ("-" + country).length;
                          if (! matchA && ! matchB) return 0;
                          if (! matchA && matchB) return 1;
                          if (! matchB && matchA) return -1;
                      }
                      if (preferredNames != null) {
                          var indexA = voices.length;
                          var indexB = voices.length;
                          preferredNames.forEach((e, i) => {
                              if (indexA == voices.length && a.name.match(e) != null) indexA = i; 
                              if (indexB == voices.length && b.name.match(e) != null) indexB = i; 
                          });
                          return indexA - indexB;
                      }
                      return 0;
                  });
                  if (voices.length > 0) msg.voice = voices[0];
                  if (language != null) {
                      msg.lang = language;
                      if (country != null) msg.lang += "-" + country;
                  }
                  msg.onend = resolvePromise;
                  window.speechSynthesis.speak(msg);
      
                  // msg.onend not triggered without call to console.log(msg)?
                  console.log(msg);
              }
      
              return promise;
          }
      
          speak("en", null, [ /Google US English/, /Samantha/, /Fiona/, /Victoria/, /female/i ], "Hello, world.")
          .then(() => speak("en", null, [ /female/i ], "Hello, world."))
          .then(() => speak("en", "US", [ /female/i ], "Hello, world."))
          .then(() => speak("en", "US", null, "Hello, world."))
          .then(() => speak("en", "GB", [ /\Wmale/i ], "Hello, world."));
      </script>
      </head>
      <body>
      </body>
      </html>
      

      【讨论】:

        【解决方案4】:

        这段代码对我有用。

          var speakObj = new SpeechSynthesisUtterance();
          speakObj.text = text;
          speakObj.voice = speechSynthesis.getVoices().filter(function(voice) {
            return voice.name == "Google UK English Female"
        
          })[0];
          window.speechSynthesis.speak(speakObj);
        

        【讨论】:

          【解决方案5】:

          经过长时间的排查,终于找到了解决办法。

          4 个人已经向我提出了一些解决方案。但这些对我不起作用。但帮助我找出解决方案。感谢他们所有人。

          为了解决这个问题,我做了两件事。

          1. 我必须在 onvoiceschanged 事件期间加载语音,如下所示:

            var voices;    
            window.speechSynthesis.onvoiceschanged = function() {
                voices=window.speechSynthesis.getVoices();
            };
            

          我从this link 找到了这个提示。

          1. “Google 英国英语女性”对我不起作用。所以我使用了“Microsoft Zira Desktop - 英语(美国)”,如下所示:

            speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];
            

          【讨论】:

            猜你喜欢
            • 2014-02-26
            • 1970-01-01
            • 2013-07-24
            • 1970-01-01
            • 1970-01-01
            • 2015-04-05
            • 2016-10-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多