TextToSpeech.setEngineByPackageName() returns success even when package is not available

為{幸葍}努か 提交于 2019-12-10 14:34:15

问题


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



来源:https://stackoverflow.com/questions/9487866/texttospeech-setenginebypackagename-returns-success-even-when-package-is-not-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!