我正在编写一段 JavaScript 代码,希望用户在单击“开始按钮”时对其表示欢迎。它可以用英语工作,但重点是我希望它用巴西葡萄牙语 (pt-BR) 说话。我尝试了很多解决方案,但似乎不起作用。有人可以帮我吗?
代码是:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<script>
startTalking = function(line){
var text = new SpeechSynthesisUtterance();
text.lang = "pt-BR";
text.text = line;
speechSynthesis.speak(text);
}
</script>
</head>
<body>
<button id="startButton" onclick = "startTalking("Bem vindo!")"></button>
</body>
</html>
当我单击按钮时,脚本会起作用,但参数中收到的文本是由英语(美国)语音朗读的。
有人知道如何修复它吗?
谢谢!!
最佳答案
感谢您的回复布鲁诺。我在发布问题的第二天就解决了这种情况,但无法在此处发布解决方案。我用这个解决了这种情况:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<script>
var text;
var voices;
window.speechSynthesis.onvoiceschanged = function() {
text = new SpeechSynthesisUtterance();
voices = window.speechSynthesis.getVoices();
text.voiceURI = 'Google português do Brasil'; //discovered after dumping getVoices()
text.lang = "pt-BR";
text.localService = true;
text.voice = voices[15]; //index to the voiceURI. This index number is not static.
}
startSpeaking = function(line){
text.text = line;
speechSynthesis.speak(text);
}
</script>
</head>
<body>
<button id="startButton" onclick = "startTalking("Bem vindo!")"></button>
</body>
</html>
一旦 onvoiceschanged 是异步的,这使得现在一切正常!
虽然我已经解决了,但还是非常感谢你的回复。非常感谢。
最诚挚的问候,
尤利西斯
关于text-to-speech - SpeechSynthesis 无法用葡萄牙语说话 (pt-BR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36485170/