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);
第一次尝试可能会发出男性的声音。但是,在第二次尝试(不刷新的情况下),它将散发出女性的声音并尝试在虚拟服务器上部署它,在第一个Go中它会像魅力一样工作。
niky在我身上,声音在该页面首次加载时不会发生变化。
我找到了一种对我有用的解决方案。
getVoices() $(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>
经过长时间的故障排除,我可以找出解决方案。4人已经向我建议了一些解决方案。但是这些对我不起作用。但帮助我找出了解决方案。感谢所有人。
i在onvoiceschanged事件期间必须加载声音,如下:
i我从thislink
.找到了这个提示。
英国英语女性'对我不起作用。因此,我使用了“ Microsoft Zira桌面 - 英语(美国)”,如下:speech.voice = voices.filter(function(voice) { return voice.name == 'Microsoft Zira Desktop - English (United States)'; })[0];
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>