【发布时间】:2019-03-29 05:27:35
【问题描述】:
我在 Android-Chrome 上使用语音合成 API。问题是尽管有 4 种英语语音可用,但无论代码指定什么,浏览器始终使用美国英语。我可以使用其他语言,例如法语,而不是其他英语语音,例如 en-AU、GB 或 IN。
此代码从 getVoices 数组中过滤英国英语语音对象,并使用第一个来说出单词“tomato”。问题是这个词总是发音为“to-may-lo”而不是“to-mar-to”,这意味着我的文字没有按应有的押韵。
显示使用的语音对象(在我尝试过的手机上)是 GB 的。
html...
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Let's not call the whole thing off</title>
<script src="tomato.js" defer></script>
</head>
<body>
<div id="tomato" lang="en-GB">Tomato</div>
<div id="platform"></div>
</body>
</html>
还有脚本...
var platform = document.getElementById("platform");
var tomato = document.getElementById("tomato");
var voices = [];
var voicesGB = [];
voices = speechSynthesis.getVoices();
speechSynthesis.onvoiceschanged = function() {
voices = speechSynthesis.getVoices();
voicesGB = voices.filter(voice => /en(-|_)GB/.test(voice.lang));
};
function speak(word) {
var msg = new SpeechSynthesisUtterance();
msg.default = false;
msg.text = word;
msg.voice = voicesGB[0];
msg.lang = voicesGB[0].lang;
window.speechSynthesis.speak(msg);
for (var p in msg.voice) {
platform.innerHTML += p + ': ' + msg.voice[p] + '<br>\n';
}
}
tomato.addEventListener("click",() => {speak('tomato');});
还有一个 jsbin:https://jsbin.com/xefukemuga/edit?html,js,output 在 Android Chrome 中运行它并点按“番茄”一词。
我已经到处搜索并尝试了各种修复方法。您如何控制 Android-Chrome 使用的语音?
【问题讨论】:
-
你修好了吗?我遇到了类似的问题并找到了解决方法,请在此处查看我的答案:stackoverflow.com/questions/56728654/…
-
好吧,我在“_”上看到了您的代码过滤器,所以也许这不是问题,除非 voicesGB[0].long 仍然有破折号。
-
@royappa 破折号或下划线的代码过滤器:/en(-|_)GB/
-
我看到了,但我认为 msg.lang 中的最终结果会与平台存储的内容相匹配,但如果不是,也许您已将 '-' 替换为 '_'?还是您已经通过其他方式解决了这个问题?我仍然对这个问题感兴趣,因为平台不遵循明确的标准是没有意义的。
-
@royappa 不,问题仍未解决。
标签: javascript text-to-speech speech-synthesis android-chrome