Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would TextToSpeech.getLanguage() *sometimes* return null?

In my app, I call TextToSpeech.getLanguage() well after instantiating TextToSpeech (~800ms according to LogCat), yet it sometimes (not always) return null, despite the language set properly in the system's TextToSpeech settings:

System Settings > Language & input > 
  Text-to-speech output > Google Text-to-speech > English (United Kingdom)

This only happens in Jelly Bean (Android 4.1.1). It doesn't happen in Android 2.2.

Is this a known Android bug? Or am I doing something wrong?

I instantiate, BTW, TextToSpeech in my app's main activity with:

new TextToSpeech(this, this); 

And both instantiation and the call to TextToSpeech.getLanguage() are made in the same thread (thus order of calls is guaranteed).

like image 515
Eternal Learner Avatar asked Oct 21 '22 02:10

Eternal Learner


2 Answers

You have to call getLanguage () in onInit (), sometimes it takes a few second for onInit () to be called.

like image 92
Hoan Nguyen Avatar answered Nov 15 '22 05:11

Hoan Nguyen


I also discovered this bug today on an Asus TF700T running Android 4.1.1. I tried to resolve the issue by calling getLanguage() again as Eternal Learner suggested in a comment, but it still returns null no matter how many times I call it.

The easiest workaround for me was to abandon the current broken instance of TextToSpeech and create a new instance. The second instance seems to work reliably.

like image 27
Brian Avatar answered Nov 15 '22 05:11

Brian