即使没有可用的package TextToSpeech.setEngineByPackageName()返回成功信息信息、package、TextToSpeech、setEngineByPackage

2023-09-08 00:21:02 作者:沐雨瞳╰

管理,使TextToSpeech.setEngineByPackageName()工作的感谢这个答案之后,我现在具有反其道而行之问题:

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); 总是返回 TextToSpeech.SUCCESS 即使当该包未在所有已安装在设备中。

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.

由于包不可用在设备上,TTS继续与Android的默认微微,预计说话,但我不明白为什么 setEngineByPackageName()返回SUCCESS。

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.

怎么会这样?

推荐答案

有关的所有,我张贴由提供了答案的好处@Nikolay Elenkov在不同(但相关的)问题的:

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

调用setEngineByPackageName()时,包不存在是不是一个好主意。相反,检查它是否已安装和不要尝试使用它,如果它没有安装

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://$c$c.google.com/p/wwwjdic/source/browse/branches/2.0/wwwjdic/src/org/nick/wwwjdic/TtsManager.java