speech-synthesis

SpeechSynthesisUtterance to use other voice than native


Trying to change the voice from native to Google US English without success. This is the code I am using:

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.');

Any clues? Thanks


Solution

  • This works:

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