如何在Google Chrome中使用Web Speech API获取女性声音

4
在网页中,我想要一个女性的声音来朗读我的文本。我尝试使用以下代码实现这一点。但是到目前为止仍然是男性的声音在说话。如何安排一个女性的声音来朗读我的文本?有人能分享给我一个在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);
5个回答

4
以下代码对我有用,希望对你也有用。
var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[3];
msg.text = "Hello World";
speechSynthesis.speak(msg);

第一次尝试时,它可能会发出男性声音。但是在第二次尝试(不刷新)后,它将发出女声,并尝试在虚拟服务器上部署,第一次尝试即可成功。


1
遇到了一些奇怪的事情:https://dev59.com/mwr5s4cB2Jgan1znZY-v - Nirojan Selvanathan
这取决于所列出的语音。您只是访问硬编码属性,如果由于任何原因voicelist更改,则会获得不同的语音。 - Jorge Fuentes González

2

在页面首次加载时,我的语音无法改变。我找到了一个适合我的解决方案。
应该在文档准备就绪时触发getVoices()。因此,我在我的js文件顶部添加了以下代码。

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

1

经过漫长的故障排除,我终于找到了解决方法。

已有4人向我提出了一些解决方案,但这些方案对我来说都不起作用。但是它们帮助我找到了解决方法。感谢他们所有人。

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

  1. I had to load voices during onvoiceschanged event as follows:

    var voices;    
    window.speechSynthesis.onvoiceschanged = function() {
        voices=window.speechSynthesis.getVoices();
    };
    
我从this link找到了这个提示。
  1. 'Google UK English Female' does not work for me. So I used 'Microsoft Zira Desktop - English (United States)' as follows:

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

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>

1
这段代码对我有效。
  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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接