如何通过Google Chrome中的Web Speech API获得女性声音

时间:2016-10-24 09:50:39

标签: web speech-synthesis

在网页中,我希望女声能说出我的文字。我尝试通过以下代码执行此操作。但是现在男声仍在说话。我怎样才能安排女声说话?任何人都可以与我分享一个适用于谷歌浏览器的正确代码。

var voices = speechSynthesis.getVoices();
var msg = new SpeechSynthesisUtterance("Hello World!");
msg.default=false; 
msg.localservice=true;
msg.lang = "en-GB";
msg.voice = voices[3].name;
speechSynthesis.speak(msg);

5 个答案:

答案 0 :(得分:5)

以下代码对我有用。我希望它适合你。

var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[3];
msg.text = "Hello World";
speechSynthesis.speak(msg);

在第一次尝试时,它可能会发出男性的声音。但是在第二次尝试(没有刷新)时,它会发出女性的声音并尝试将其部署在虚拟服务器上,在那里它将像第一次使用时一样充满魅力。

答案 1 :(得分:2)

我发现在第一次加载页面时语音不会改变。

我找到了一个适合我的解决方案 文档准备好后,应触发 getVoices() 所以我在这个js的顶部添加这样的东西。

{{1}}

答案 2 :(得分:1)

在Chrome上,我做了类似的事情:

<html>
<head>
<script>
    function speak(language, country, preferredNames, text) {
        var resolvePromise;
        var promise = new Promise(r => resolvePromise = r);

        var voices = window.speechSynthesis.getVoices();
        if (voices.length == 0) {
            new Promise(r => {
                window.speechSynthesis.onvoiceschanged = () => { 
                        window.speechSynthesis.onvoiceschanged = null;
                        r(); 
                    };
            })
            .then(() => speak(language, country, preferredNames, text))
            .then(resolvePromise);
        } else {
            var msg = new SpeechSynthesisUtterance(text);
            voices.sort((a, b) => {
                if (language != null) {
                    var matchA = a.lang.indexOf(language + "-") == 0;
                    var matchB = b.lang.indexOf(language + "-") == 0;
                    if (! matchA && ! matchB) return 0;
                    if (! matchA && matchB) return 1;
                    if (! matchB && matchA) return -1;
                }
                if (country != null) {
                    var matchA = a.lang.indexOf("-" + country) == a.lang.length - ("-" + country).length;
                    var matchB = b.lang.indexOf("-" + country) == b.lang.length - ("-" + country).length;
                    if (! matchA && ! matchB) return 0;
                    if (! matchA && matchB) return 1;
                    if (! matchB && matchA) return -1;
                }
                if (preferredNames != null) {
                    var indexA = voices.length;
                    var indexB = voices.length;
                    preferredNames.forEach((e, i) => {
                        if (indexA == voices.length && a.name.match(e) != null) indexA = i; 
                        if (indexB == voices.length && b.name.match(e) != null) indexB = i; 
                    });
                    return indexA - indexB;
                }
                return 0;
            });
            if (voices.length > 0) msg.voice = voices[0];
            if (language != null) {
                msg.lang = language;
                if (country != null) msg.lang += "-" + country;
            }
            msg.onend = resolvePromise;
            window.speechSynthesis.speak(msg);

            // msg.onend not triggered without call to console.log(msg)?
            console.log(msg);
        }

        return promise;
    }

    speak("en", null, [ /Google US English/, /Samantha/, /Fiona/, /Victoria/, /female/i ], "Hello, world.")
    .then(() => speak("en", null, [ /female/i ], "Hello, world."))
    .then(() => speak("en", "US", [ /female/i ], "Hello, world."))
    .then(() => speak("en", "US", null, "Hello, world."))
    .then(() => speak("en", "GB", [ /\Wmale/i ], "Hello, world."));
</script>
</head>
<body>
</body>
</html>

答案 3 :(得分:0)

这段代码对我有用。

  var speakObj = new SpeechSynthesisUtterance();
  speakObj.text = text;
  speakObj.voice = speechSynthesis.getVoices().filter(function(voice) {
    return voice.name == "Google UK English Female"

  })[0];
  window.speechSynthesis.speak(speakObj);

答案 4 :(得分:0)

经过长时间的故障排除,我可以找到解决方案。

4个人已经建议我一些解决方案。但是这些对我没有用。但是帮助我找出了解决方案。感谢所有人。

为了解决这个问题,我做了两件事。

  1. 在onvoiceschanged事件期间,我必须加载声音,如下所示:

    var voices;    
    window.speechSynthesis.onvoiceschanged = function() {
        voices=window.speechSynthesis.getVoices();
    };
    

我从this link找到了这个提示。

  1. “ Google UK English Female”不适用于我。因此,我使用了“ Microsoft Zira Desktop-英文(美国)”,如下所示:

    speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];