如何在 Google Chrome 中设置 SpeechSynthesisUtterance 的默认语音?
How do I set the default voice for SpeechSynthesisUtterance in Google Chrome?
我有一个 Javascript 应用程序,它使用 SpeechSynthesisUtterance
进行文字转语音。在某些浏览器上,它使用了错误的语音(语言)。例如,在一个语言设置为英语的浏览器上,TTS 最终使用德语语音。
是否有设置所用语音的配置选项?
speech API spec表示浏览器可以决定默认使用哪种语音,并且每种话语语言可能有不同的默认语音。
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.
默认的话语语言由HTML lang
属性决定:
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 computed and used when the
input request opens a connection to the recognition service.
这意味着,默认使用英国语音:
<html lang="en-GB">
<body>
<script>
var utterance = new SpeechSynthesisUtterance('Toodle pip');
window.speechSynthesis.speak(utterance);
</script>
</body>
</html>
但是,这并没有改变我在 Chrome 46 中的默认语音(我的默认语言是 en-GB
;默认情况下我也有德语语音)。
你 可以 使用 navigator.language
作为默认的话语语言,但根据 this answer 它不是浏览器设置的可靠指标(对我来说计算结果为 en-US
,它在我的语言列表中,但不是我的默认语言)。
Chrome 似乎忽略了 HTML 语言 属性。解决办法是设置utterance的属性:
<script>
var utterance = new SpeechSynthesisUtterance('Toodle pip');
utterance.lang='en-US'; // for US english, en-GR for british
window.speechSynthesis.speak(utterance);
</script>
我有一个 Javascript 应用程序,它使用 SpeechSynthesisUtterance
进行文字转语音。在某些浏览器上,它使用了错误的语音(语言)。例如,在一个语言设置为英语的浏览器上,TTS 最终使用德语语音。
是否有设置所用语音的配置选项?
speech API spec表示浏览器可以决定默认使用哪种语音,并且每种话语语言可能有不同的默认语音。
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.
默认的话语语言由HTML lang
属性决定:
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 computed and used when the input request opens a connection to the recognition service.
这意味着,默认使用英国语音:
<html lang="en-GB">
<body>
<script>
var utterance = new SpeechSynthesisUtterance('Toodle pip');
window.speechSynthesis.speak(utterance);
</script>
</body>
</html>
但是,这并没有改变我在 Chrome 46 中的默认语音(我的默认语言是 en-GB
;默认情况下我也有德语语音)。
你 可以 使用 navigator.language
作为默认的话语语言,但根据 this answer 它不是浏览器设置的可靠指标(对我来说计算结果为 en-US
,它在我的语言列表中,但不是我的默认语言)。
Chrome 似乎忽略了 HTML 语言 属性。解决办法是设置utterance的属性:
<script>
var utterance = new SpeechSynthesisUtterance('Toodle pip');
utterance.lang='en-US'; // for US english, en-GR for british
window.speechSynthesis.speak(utterance);
</script>