SpeechSynthesisUtterance 使用非本地语音

SpeechSynthesisUtterance to use other voice than native

尝试将声音从 native 更改为 Google US English 但没有成功。这是我使用的代码:

https://jsfiddle.net/uv2k0qws/

function speak(text) {
    var msg = new SpeechSynthesisUtterance();
    var voices = speechSynthesis.getVoices();
    msg.volume = 1;
    msg.rate = 1;
    msg.pitch = 2;
    msg.text = text;
    msg.lang = "en-US";
    msg.name = "Google US English";
    msg.voiceURI = "Google US English"
    speechSynthesis.speak(msg);
}
speak('Attention! This is a test.');

有线索吗?谢谢

这个有效:

    var utterance = new SpeechSynthesisUtterance();

    utterance.onstart = function (event) {
        console.log('The utterance started to be spoken.')
    };

    window.speechSynthesis.onvoiceschanged = function () {

        voices = window.speechSynthesis.getVoices();
        utterance.voice = voices.filter(function (voice) { return voice.lang == 'pt-BR'; })[0];

    }


    $(document).ready(function () {
        utterance.text = "Bom dia amigos!";
        window.speechSynthesis.speak(utterance)
    })

请注意 utterance.voice 在 Android Chrome 上的设置还不够。尽管它似乎适用于所有其他平台。您还必须设置 utterance.lang。这是一个片段,您可能需要在控制台中 运行 两次才能看到它工作,因为 speechSynthesis.getVoices 是延迟加载的。

let utterance = new SpeechSynthesisUtterance("hello");
let voice = speechSynthesis.getVoices()[0]
utterance.voice = voice; // required for iOS
utterance.lang = voice.lang; // required for Android Chrome
utterance.voiceURI = voice.voiceURI;
speechSynthesis.speak(utterance);