如何在Google Chrome中通过Web语音API获得女性语音
在网页中,我希望有一个女性的声音来表达我的文字。我试图通过以下代码来实现这一点。但现在还是有男声在说话。我如何安排一个女性的声音来谈论我的文章?有人能给我一个在谷歌浏览器上运行的正确代码吗如何在Google Chrome中通过Web语音API获得女性语音,web,speech-synthesis,Web,Speech Synthesis,在网页中,我希望有一个女性的声音来表达我的文字。我试图通过以下代码来实现这一点。但现在还是有男声在说话。我如何安排一个女性的声音来谈论我的文章?有人能给我一个在谷歌浏览器上运行的正确代码吗 var voices = speechSynthesis.getVoices(); var msg = new SpeechSynthesisUtterance("Hello World!"); msg.default=false; msg.localservice=true; msg.lang = "en
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>
函数speak(语言、国家、首选名称、文本){
无功承诺;
var承诺=新承诺(r=>resolvePromise=r);
var voices=window.speechSynthesis.getVoices();
如果(voices.length==0){
新承诺(r=>{
window.speechSynthesis.onvoiceschanged=()=>{
window.speechSynthesis.onvoiceschanged=null;
r();
};
})
.然后(()=>说(语言、国家、首选名称、文本))
.然后(承诺);
}否则{
var msg=新演讲(文本);
voices.sort((a,b)=>{
if(语言!=null){
var matchA=a.lang.indexOf(语言+“-”)==0;
var matchB=b.lang.indexOf(语言+“-”)==0;
如果(!matchA&&!matchB)返回0;
如果(!matchA&&matchB)返回1;
如果(!matchB&&matchA)返回-1;
}
如果(国家/地区!=null){
var matchA=a.lang.indexOf(“-”+国家)==a.lang.length-(“-”+国家).length;
var matchB=b.lang.indexOf(“-”+国家)==b.lang.length-(“-”+国家).length;
如果(!matchA&&!matchB)返回0;
如果(!matchA&&matchB)返回1;
如果(!matchB&&matchA)返回-1;
}
if(preferredNames!=null){
var indexA=语音长度;
var indexB=voices.length;
首选名称。forEach((e,i)=>{
如果(indexA==voices.length&&a.name.match(e)!=null)indexA=i;
如果(indexB==voices.length&&b.name.match(e)!=null)indexB=i;
});
返回indexA-indexB;
}
返回0;
});
如果(voices.length>0)msg.voice=voices[0];
if(语言!=null){
msg.lang=语言;
如果(country!=null)msg.lang+=“-”+国家;
}
msg.onend=resolvePromise;
window.speechSynthesis.speak(msg);
//在未调用console.log(msg)的情况下未触发msg.oned?
控制台日志(msg);
}
回报承诺;
}
说(“en”,空,[/Google-US-English/,/Samantha/,/Fiona/,/Victoria/,/female/i],“你好,世界。”)
.然后(()=>说(“en”,空,[/female/i],“你好,世界。”)
.然后(()=>说(“恩”,“我们”,“女性/我”,“你好,世界”)
.然后(()=>说(“恩”,“我们”,空,“你好,世界”)
。然后(()=>说(“en”,“GB”,“[/\Wmale/i],“你好,世界”);
这个代码对我有用
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个人已经向我提出了一些解决方案。但这些对我不起作用。但是帮助我找到了解决办法。感谢他们所有人 为了解决这个问题,我做了两件事
var voices;
window.speechSynthesis.onvoiceschanged = function() {
voices=window.speechSynthesis.getVoices();
};
speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];
var voices;
window.speechSynthesis.onvoiceschanged = function() {
voices=window.speechSynthesis.getVoices();
};
speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];