如何抑制 TTS.getAvailableLocales() 的警告信息

How to suppress TTS.getAvailableLocales()'s warning messages

我可以像这样迭代获取当前设备支持的语言环境列表:

for (Locale each : Locale.getAvailableLocales())
    if(1 <= myTTS.isLanguageAvailable(each))
        supportedLocales.add(each);

但我的问题是,方法 isLanguageAvailable() 总是记录不支持的语言环境的警告消息,如下所示:

08-15 20:10:08.163  5056    5672    W   TTS Local voice not installed, we will not be able to do local fallback
08-15 20:10:08.527  5056    5067    W   TTS No local or network voice found, failing dispatch
08-15 20:10:08.527  5056    5067    W   TTS Could not find voice for lkt_US
08-15 20:10:08.606  24353   24382   W   TextToSpeech    Couldn't retrieve ISO 3166 country code for locale: es_EA
08-15 20:10:08.606  24353   24382   W   TextToSpeech    java.util.MissingResourceException: Couldn't find 3-letter country code for EA
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at java.util.Locale.getISO3Country(Locale.java:1720)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at android.speech.tts.TextToSpeech.run(TextToSpeech.java:1789)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at android.speech.tts.TextToSpeech.run(TextToSpeech.java:1777)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at android.speech.tts.TextToSpeech$Connection.runAction(TextToSpeech.java:2301)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at android.speech.tts.TextToSpeech.runAction(TextToSpeech.java:752)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at android.speech.tts.TextToSpeech.runAction(TextToSpeech.java:742)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at android.speech.tts.TextToSpeech.isLanguageAvailable(TextToSpeech.java:1776)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at com.jyplugin.ttsforandroid.TTSEngine.getAvailableLanguageNames(TTSEngine.java:155)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at com.jyplugin.ttsforandroid.TTSEngine.getAvailableDisplayNames(TTSEngine.java:190)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at com.jyplugin.ttsforandroid.MainActivity.run(MainActivity.java:45)
08-15 20:10:08.606  24353   24382   W   TextToSpeech        at java.lang.Thread.run(Thread.java:764)

每当我启动我的 TTS 应用程序时,我都会得到一堆这样的东西,我真的不想要这个。

如何为我的发布版本抑制这些不需要的警告消息?

一种解决方法是将 logcat 过滤器设置为除 TextToSpeech 之外的所有内容(您需要启用正则表达式):^((?! TextToSpeech).)*$

我认为您无法对来自核心 api 的日志做任何事情。当您从某些手机进行调试并始终获取蓝牙或连接日志时,会出现同样的问题。

自 API 21 起,您可以调用 [您的 tts 实例].getAvailableLanguages(),它将 return 一组受支持的语言环境,而不是迭代您正在做的方式。

不过,需要注意的一件重要事情是,getAvailableLanguages() 方法不能保证提供格式正确的语言环境...例如,Samsung TTS 版本 201503021 运行 on API 23 将return 三字母语言和国家代码。德普

这意味着您不必继续使用这些相同的 Locale 对象来执行 setLanguage() 或 isLanguageAvailable()... 因为 TTS 对象 do依赖您提供的区域设置是否正确格式化。 :(

Google 引擎似乎提供了正确的语言环境,但是...如果您有一组从系统中获取的语言环境,例如:

    // Get all locales from user's preferred list (API 24+) or the single preferred locale (API<24)
    LocaleListCompat systemLocaleList = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());

...然后您可以像现在一样循环执行 isLanguageAvailable()。