我如何通过网络演讲API在Google Chrome中获得女性声音。

问题描述 投票:0回答:5
在一个网页中,我希望女性的声音会说我的文字。我试图通过以下代码来执行此操作。但是现在男性的声音在说话。我如何安排女性的声音来谈论我的文字?任何人都可以分享我在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);

	
web speech-synthesis
5个回答
3
投票
var msg = new SpeechSynthesisUtterance(); var voices = window.speechSynthesis.getVoices(); msg.voice = voices[3]; msg.text = "Hello World"; speechSynthesis.speak(msg);

第一次尝试可能会发出男性的声音。但是,在第二次尝试(不刷新的情况下),它将散发出女性的声音并尝试在虚拟服务器上部署它,在第一个Go中它会像魅力一样工作。

niky在我身上,声音在该页面首次加载时不会发生变化。 

我找到了一种对我有用的解决方案。

getVoices()

2
投票

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


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
投票

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

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

1
投票
要解决这个问题,我做了两件事。

i在onvoiceschanged事件期间必须加载声音,如下:

1
投票

i我从thislink

.

找到了这个提示。

英国英语女性'对我不起作用。因此,我使用了“ Microsoft Zira桌面 - 英语(美国)”,如下:
  1. speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];

        
在处理我的react.js项目时,我发现您必须用整个对象引用它们,而不仅仅是名称。这是我在代码中使用它的方式,并且它起作用了……唯一的是,在名为Hubert的声音中有一个第四个元素,它似乎不起作用,因此您可以通过删除该数组的最后一个元素来选择退出,就像我通过切片映射的数组“(0,-1)”。

const [selectedVoice, setSelectedVoice] = useState(); const handleVoiceChange = (e) => { const selectedVoiceIndex = e.target.value; const voices = window.speechSynthesis.getVoices().slice(0,-1); const voice = voices[selectedVoiceIndex] console.log("current voice:", voice.name); if (voice) { setSelectedVoice(voice); } else { console.warn("Selected voice not found."); } };

    <select onChange={handleVoiceChange}> {window.speechSynthesis.getVoices().slice(0,-1).map((voice, index) => ( <option key={index} value={index}> {voice.name} </option> ))} </select>
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.