如何通过网络语音 API 在 Google Chrome 中获得女声

How can I get female voice by Web Speech API in Google Chrome

在网页中,我想要一个女声来朗读我的文本。我试图通过以下代码来做到这一点。但现在男声还在说话。我怎样才能安排一个女声来谈论我的文字?谁能给我分享一个在 Google Chrome.

中有效的正确代码
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);

下面的代码对我有用。希望对你有用。

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

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

在 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>

这段代码对我有用。

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

我遇到这种情况,第一次加载页面时声音没有改变。

我找到了适合我的解决方案。
getVoices() 应在文档准备就绪时触发。
所以我像这样在我的 js 顶部添加。

   $(document).ready(function() {
        var voices = window.speechSynthesis.getVoices();
    })

经过长时间的排查,我找到了解决办法。

4 个人已经向我提出了一些解决方案。但这些对我不起作用。但帮助我找出解决方案。感谢他们所有人。

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

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

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

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

  1. 'Google UK English Female' 对我不起作用。所以我使用 'Microsoft Zira Desktop - English (United States)' 如下:

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