androidtext-to-speech

TextToSpeech.setEngineByPackageName() triggers NullPointerException


My activity's onInit() contains a call to TextToSpeech.setEngineByPackageName():

  tts = new TextToSpeech(this, this);
  tts.setEngineByPackageName("com.ivona.tts.voicebeta.eng.usa.kendra");

It works on an Android 2.2.2 device, but on an Android 2.3.4 device it produced a NullPointerException, with the following stack trace:

 E/TextToSpeech.java - setEngineByPackageName(3423): NullPointerException
 W/System.err(3423): java.lang.NullPointerException
 W/System.err(3423):    at android.os.Parcel.readException(Parcel.java:1328)
 W/System.err(3423):    at android.os.Parcel.readException(Parcel.java:1276)
 W/System.err(3423):    at android.speech.tts.ITts$Stub$Proxy.setEngineByPackageName(ITts.java:654)
 W/System.err(3423):    at android.speech.tts.TextToSpeech.setEngineByPackageName(TextToSpeech.java:1356)

Since I am providing a hard-coded string parameter, I know that the parameter isn't what's causing the NullPointerException.

I also know that setEngineByPackageName() is deprecated but that's only since API 14, so this couldn't be the reason.

Any idea what could be causing this NullPointerException?

EDIT: I wouldn't have been concerned with the "why" if this didn't result in an endless bombardment of:

I/TextToSpeech.java(3652): initTts() successfully bound to service

Followed by calls to onInit() (by the system, not by my code).

My hope is that if I underderstand why this is happening, I can stop the bombardment of onInit()s and recover gracefully from the error.


Solution

  • Is the TTS engine you are referencing installed on the 2.3.4 device? If it is, it might be a platform bug.

    EDIT: Don't remember what results I got when I did this, but calling setEngineByPackageName() when the package doesn't exists is not a good idea. Check if it is installed and don't try to use it if it's not. Something like:

    boolean isPackageInstalled(String packageName) {
      PackageManager pm = context.getPackageManager();
      try {
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
    
        return pi != null;
      } catch (NameNotFoundException e) {
        return false;
      }
    }