admin 管理员组文章数量: 1086019
I have a Javascript app that uses SpeechSynthesisUtterance
for text to speech. On some browsers it uses the wrong voice (language). For example, on one browser whose language is set to English, the TTS ends up using a German voice.
Is there a configuration option to set the voice used?
I have a Javascript app that uses SpeechSynthesisUtterance
for text to speech. On some browsers it uses the wrong voice (language). For example, on one browser whose language is set to English, the TTS ends up using a German voice.
Is there a configuration option to set the voice used?
Share Improve this question edited Nov 11, 2015 at 22:24 joews 30.4k11 gold badges81 silver badges91 bronze badges asked Nov 11, 2015 at 21:57 ZiffusionZiffusion 8,9434 gold badges33 silver badges59 bronze badges 1- 1 Sounds like this bug reported for Chrome OS code.google./p/chromium/issues/detail?id=453584 – Sarah Elan Commented Nov 12, 2015 at 14:13
2 Answers
Reset to default 4The speech API spec says that browsers can decide which voice to use by default, and that each utterance language may have a different default voice.
voice default
attribute:
This attribute is true for at most one voice per language. There may be a different default for each language. It is user agent dependent how default voices are determined.
The default utterence language is decided by the HTML lang
attribute:
utterance lang
attribute:
This attribute specifies the language of the speech synthesis for the utterance, using a valid BCP 47 language tag. [BCP47] If unset it remains unset for getting in script, but will default to use the lang of the html document root element and associated hierachy. This default value is puted and used when the input request opens a connection to the recognition service.
This implies that, to use a British voice by default:
<html lang="en-GB">
<body>
<script>
var utterance = new SpeechSynthesisUtterance('Toodle pip');
window.speechSynthesis.speak(utterance);
</script>
</body>
</html>
However, this did not change the default voice for me in Chrome 46 (my default language is en-GB
; I also get a German voice by default).
You could use navigator.language
as the default utterance language, but according to this answer it is not a reliable indicator of browser settings (for me it evaluates to en-US
, which is in my list of languages but is not my default).
Chrome seems to ignore the HTML lang property. The solution is to set the property of the utterance:
<script>
var utterance = new SpeechSynthesisUtterance('Toodle pip');
utterance.lang='en-US'; // for US english, en-GR for british
window.speechSynthesis.speak(utterance);
</script>
本文标签: javascriptHow do I set the default voice for SpeechSynthesisUtterance in Google ChromeStack Overflow
版权声明:本文标题:javascript - How do I set the default voice for SpeechSynthesisUtterance in Google Chrome? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743998481a2516011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论