【问题标题】:text to speech web api results in error on voice setting文本到语音 Web api 导致语音设置错误
【发布时间】:2021-10-22 23:56:34
【问题描述】:

点击按钮我想说blue skyen-US语言
此代码导致utterThis.voice 行出错
请帮忙

var synth = window.speechSynthesis;

$('button').on('click', function(){
    var utterThis = new SpeechSynthesisUtterance('blue sky');
        utterThis.rate = 1;
        utterThis.pitch = 1;
        utterThis.voice = {voiceURI: 'Google US English', name: 'Google US English', lang: 'en-US', localService: false, default: false};
    synth.speak(utterThis);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>CLICK</button>

【问题讨论】:

    标签: javascript jquery text-to-speech webapi


    【解决方案1】:

    您需要使用 .getVoices 返回的声音之一 - 将普通对象分配给 .voice 将不起作用。

    $('button').on('click', function() {
      var utterThis = new SpeechSynthesisUtterance('blue sky');
      utterThis.rate = 1;
      utterThis.pitch = 1;
    
      for (const v of speechSynthesis.getVoices()) {
        if (v.voiceURI === 'Google US English') {
          utterThis.voice = v;
        }
      }
      speechSynthesis.speak(utterThis);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button>CLICK</button>

    .getVoices 返回的声音由要支持的浏览器/操作系统验证,并且明显与将文本转换为语音的底层服务相关联 - 另一方面,普通对象没有,这这就是为什么普通对象不起作用的原因。

    【讨论】:

    • 非常感谢您的解释。我在参考页面上没有找到任何关于此的注释
    • 参见here - 这应该设置为SpeechSynthesis.getVoices()返回的 SpeechSynthesisVoice 对象之一
    • 我明白了,但是直接设置对象不起作用仍然很奇怪
    猜你喜欢
    • 2023-03-03
    • 2017-10-29
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多