Reputation: 8923
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?
Upvotes: 5
Views: 7584
Reputation: 41
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>
Upvotes: 2
Reputation: 30330
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.
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:
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.
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).
Upvotes: 4