TextToSpeech.setEngineByPackageName() returns succ

2019-06-21 05:22发布

问题:

After managing to make TextToSpeech.setEngineByPackageName() work thanks to this answer, I am now having the "opposite" problem:

tts.setEngineByPackageName("com.ivona.tts.voicebeta.eng.usa.kendra"); always returns TextToSpeech.SUCCESS, even when that package is not installed at all in the device.

Since the package is not available on the device, TTS proceeds to speak with Android's default pico, which is expected, but I don't understand why setEngineByPackageName() returns SUCCESS.

How could this be?

回答1:

For the benefit of all, I am posting the answer provided by @Nikolay Elenkov on a different (but related) question:

Calling setEngineByPackageName() when the package doesn't exists is not a good idea. Instead, check if it is installed and don't try to use it if it's not installed:

boolean isPackageInstalled(String packageName) {
  PackageManager pm = context.getPackageManager();
  try {
    PackageInfo pi = pm.getPackageInfo(packageName, 0);

    return pi != null;
  } catch (NameNotFoundException e) {
    return false;
  }
}

A good example of how this is done can be seen at:

http://code.google.com/p/wwwjdic/source/browse/branches/2.0/wwwjdic/src/org/nick/wwwjdic/TtsManager.java