Female voice in google chrome speechSynthesis

ε祈祈猫儿з 提交于 2019-12-24 10:59:47

问题


Im using this exact code in both the scenarios.

        var msg = new SpeechSynthesisUtterance();
        var voices = window.speechSynthesis.getVoices();
        msg.voice = voices[1];
        msg.text = "hello world";
        msg.lang = 'en-US';
        speechSynthesis.speak(msg);

If I run this in the chrome console I get a female voice. But if I put the exact code in an index.html and run it, it plays a male voice. Could any one please clarify why this difference occurs. Thanks in advance.


回答1:


Found the Root cause. Getting the list of voices in speechSynthesis of Chrome (Web Speech API)

The calls are async, so when I try to run in index.html the voices array is empty. As I suggested when I run this and then use the speak it works fine.

    var msg;
    var voices;
    var timer = setInterval(function() {
        voices = speechSynthesis.getVoices();
        console.log(voices);
        if (voices.length !== 0) {
            msg = new SpeechSynthesisUtterance();
            msg.voice = voices[0];
            speechSynthesis.speak(msg);
            msg.lang = 'en-US';
            clearInterval(timer);
        }
    }, 200);
    timer();
    speechSynthesis.speak("hello world");


来源:https://stackoverflow.com/questions/45877555/female-voice-in-google-chrome-speechsynthesis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!