最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I set the default voice for SpeechSynthesisUtterance in Google Chrome? - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

The 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>
发布评论

评论列表(0)

  1. 暂无评论