10

I was playing with Chrome's speechSynthesis

msg = new SpeechSynthesisUtterance("some text");
msg.lang = "fr-FR" ;
window.speechSynthesis.speak(msg);  

when I discovered this strange issue : it can't speak anymore if your device is offline. I searched the web but did not find any explanation.
Can't this work offline ?
(If you know the reason why it needs to be online, please tell me it too in the comments.)

Diego
  • 569
  • 1
  • 9
  • 28

1 Answers1

13

Speech synthesis voices are either local on the device or come from remote speech synthesizer services. If the voice is a remote service, the browser will only be able to use it if it is online and can connect to it.

You don't say which environment you are on, but the Google Français voice that would be used for fr-FR on Windows and OS X is a remote service, so it doesn't work offline.

You can check which voices are available on a device by calling speechSynthesis.getVoices() and checking the localService property for each voice.

Sarah Elan
  • 2,465
  • 1
  • 23
  • 45
  • And now the real question is, how to add localService enabled voices :/ – Király István Mar 30 '18 at 03:14
  • It's slightly weird: I am getting an empty getVoices() array - but English speech synthesis works fine. (Using Chrome on Linux.) – Stefan Reich Apr 02 '20 at 17:18
  • @StefanReich The `getVoices()` method is weird, you need to call it once to trigger the loading of the voices, then wait a bit, and then call it again, and then you'll see the voices. Try pasting this in your console: `speechSynthesis.getVoices(); await new Promise(r => setTimeout(r, 2000)); console.log(speechSynthesis.getVoices())` (i.e. get voices, wait for 2000 milliseconds, log the voices). Rather than waiting an arbitrary amount of time (which might not be enough), you can use the `onvoiceschanged` event: https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/onvoiceschanged – joe Aug 14 '20 at 04:58
  • Oh, nice tip, I'll try @joe – Stefan Reich Aug 14 '20 at 11:30
  • I should have noted that if you're offline, and on Linux, then a [Chromium bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1057775) currently prevents local/native (espeak) voices from being loaded, so there will be no voices loaded at all, and you'll get an empty array no matter what - at least on Chrome/Chromium 84 and Ubuntu 20.04. – joe Aug 14 '20 at 11:37