【发布时间】:2021-07-31 04:01:28
【问题描述】:
我的以下 HTML 页面正确地朗读了文本,但它不是用女性声音朗读 Microsoft Zira Desktop - English (United States)。 问题:我可能在这里遗漏了什么,我们怎样才能让它以女性的声音说话?
备注:我在MS Edge 和Google Chrome 中多次尝试了这个html,无论是否刷新页面,但它一直用相同的男性声音说话。似乎它忽略了下面 JavaScript 中的 msg.voice 值。我正在使用Windows 10 - 这可能不重要。
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
var msg = new SpeechSynthesisUtterance();
msg.voice = speechSynthesis.getVoices().filter(function(voice) {
return voice.name == "Microsoft Zira Desktop - English (United States)"})[0];
msg.text = document.getElementById("testDiv").textContent;
window.speechSynthesis.speak(msg);
}
</script>
</head>
<body>
<h2>JavaScript in Head</h2>
<div id="testDiv">SQL Managed Instance gives you an instance of SQL Server but removes much of the <b>overhead</b> of managing a <u>virtual machine</u>. Most of the features available in SQL Server are available in SQL Managed Instance. This option is ideal for customers who want to use instance-scoped features and want to move to Azure without rearchitecting their applications.</div>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
更新
根据用户 @Frazer 的建议,我在我的 google chrome 控制台中运行 speechSynthesis.getVoices() 并得到以下结果 - 其中确实包含 Microsoft Zira .... 语音:
观察:
按照this 的建议,我将<script> 块移到了主体块的末尾(就在</body> 之前),但仍然是相同的男声。 但是,当我将声音从Microsoft Zira Desktop - English (United States) 替换为Google UK English Female 时,会发生以下情况:在第一次点击Try it 按钮时,扬声器仍然是默认男性,但在随后的每一次点击中在这个按钮上,我正确地得到了Google UK English Female 的声音。注意:Microsoft Zira Desktop - English (United States) 在上述场景中没有任何作用。这让我相信这项技术仍然是体验式的——正如 here 所述。
【问题讨论】:
-
您的代码对我有用。如果您在控制台中运行 SpeechSynthesis.getVoices() 会发生什么? Zira 是回归的声音之一吗?
-
@Frazer 感谢您调查此问题。根据您的建议,我在控制台中运行了
speechSynthesis.getVoices(),它确实包含 zira。我添加了一个 UPDATE 部分来回答您的问题。 -
如果您尝试其中一种 Google 语音会发生什么,例如“谷歌英国英语女性”?
-
@Frazer 仍然完全相同(可能是 Windows 10 上的默认设置)男性声音。在其他在线应用程序上,当我从下拉菜单中选择不同的声音(男性或女性)时,它会正确地使用所选声音说话。它只是没有发生在我的本地 html 文件上(如上所示)。
-
如果在控制台中按顺序运行这些会发生什么?你得到什么输出?:speechSynthesis.getVoices(); const u = new SpeechSynthesisUtterance('1'); u.voice = SpeechSynthesis.getVoices()[2];语音合成.speak(u);
标签: javascript text-to-speech speech-synthesis webspeech-api