文本到语音网络 api 导致语音设置错误

text to speech web api results in error on voice setting

单击按钮我想用 en-US 语言说 blue sky
此代码导致 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>

您需要使用 .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 返回的语音由要支持的浏览器/操作系统验证,并且明确绑定到将文本转换为语音的底层服务 - 另一方面,一个普通对象,不会,这就是普通对象不起作用的原因。