SpeechSynthesisUtterance使用除本地

时间:2016-10-28 15:40:30

标签: speech-synthesis

尝试将语音从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.');

任何线索?感谢

2 个答案:

答案 0 :(得分:1)

这有效:

    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)
    })

答案 1 :(得分:0)

请注意,在 Android Chrome 上设置 utterance.voice 是不够的。虽然它似乎适用于所有其他平台。您还必须设置 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);