获取文本到语音(TTS)的语言环境

我正在做一个flashcard程序的文本到语音的实现。 应该读出不同语言的文本。 为了正确地做到这一点,用户必须select要阅读的文本的语言(以后将被存储和使用,而没有问题)。

是否有可能在Android系统上获得可用的TTS语言? 如果没有,是否有可能在系统上获得所有可用的语言环境


我猜,我明白了: getAvailableLocales()tts.isLocaleAvailable(locale)

Solutions Collecting From Web of "获取文本到语音(TTS)的语言环境"

您可以使用以下functionfind设备的所有可用区域设置。

 Locale loc = new Locale("en"); Log.i("-------------",Arrays.toString(loc.getAvailableLocales())); 

产量

 I/-------------( 4390): [ar, ar_EG, bg, bg_BG, ca, ca_ES, cs, cs_CZ, da, da_DK, de, de_AT, de_BE, de_CH, de_DE, de_LI, de_LU, el, el_CY, el_GR, en, en _AU, en_BE, en_BW, en_BZ, en_CA, en_GB, en_HK, en_IE, en_IN, en_JM, en_MH, en_MT, en_NA, en_NZ, en_PH, en_PK, en_RH, en_SG, en_TT, en_US, en_US_POSIX, en_VI, en_ZA, en_ZW, es, es_AR, es_BO, es_CL, es_CO, es_CR, es_DO, es_EC, es_ES, es_GT, es_HN, es_MX, es_NI, es_PA, es_PE, es_PR, es_PY, es_SV, es_US , es_UY, es_VE, et, et_EE, eu, eu_ES, fa, fa_IR, fi, fi_FI, fr, fr_BE, fr_CA, fr_CH, fr_FR, fr_LU, fr_MC, gl, gl_ES, hr, hr_HR, hu, hu_HU, in, in_ID, is, is_IS, it, it_CH, it_IT, iw, iw_IL, ja, ja_JP, kk, kk_KZ, ko, ko_KR, lt, lt_LT, lv, lv_LV, mk, mk_MK, ms, ms_BN, ms_MY, nl, nl_BE, nl_NL, no, no_N O, no_NO_NY, pl, pl_PL, pt, pt_BR, pt_PT, ro, ro_RO, ru, ru_RU, ru_UA, sh, sh_BA, sh_CS, sh_YU, sk, sk_SK, sl, sl_SI, sq, sq_AL, sr, sr_BA, sr_ME, sr_ RS, sv, sv_FI, sv_SE, th, th_TH, tr, tr_TR, uk, uk_UA, vi, vi_VN, zh, zh_CN, zh_HK, zh_HANS_SG, zh_HANT_MO, zh_MO, zh_TW] 

其他人已经完成了艰苦的工作,在http://kaviddiss.com/2012/08/12/android-text-to-speech-languages/

为了节省您的时间,这里是他们的代码提取

 TextToSpeech tts = ... // let's assume tts is already inited at this point: Locale[] locales = Locale.getAvailableLocales(); List<Locale> localeList = new ArrayList<Locale>(); for (Locale locale : locales) { int res = tts.isLanguageAvailable(locale); if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) { localeList.add(locale); } } // at this point the localeList object will contain // all available languages for Text to Speech 

结果取决于select了哪个TTS引擎。 例如,我的一部手机包括Pico-TTS和Google-text-to-speech引擎。

Q-Smart(带有Google TTS作为选定引擎的越南语手机)

 D/SpeakRepeatedly( 3979): Engine Google Text-to-speech Engine:com.google.android.tts D/SpeakRepeatedly( 3979): Engine Pico TTS:com.svox.pico D/SpeakRepeatedly( 3979): German (Germany):German:de_DE D/SpeakRepeatedly( 3979): English (United Kingdom):English:en_GB D/SpeakRepeatedly( 3979): English (United States):English:en_US D/SpeakRepeatedly( 3979): English (United States,Computer):English:en_US_POSIX D/SpeakRepeatedly( 3979): Spanish (Spain):Spanish:es_ES D/SpeakRepeatedly( 3979): French (France):French:fr_FR D/SpeakRepeatedly( 3979): Italian (Italy):Italian:it_IT D/SpeakRepeatedly( 3979): Portuguese (Brazil):Portuguese:pt_BR D/SpeakRepeatedly( 3979): Portuguese (Portugal):Portuguese:pt_PT 

并与Picoselect

 D/SpeakRepeatedly( 4837): Engine Google Text-to-speech Engine:com.google.android.tts D/SpeakRepeatedly( 4837): Engine Pico TTS:com.svox.pico D/SpeakRepeatedly( 4837): German (Germany):German:de_DE D/SpeakRepeatedly( 4837): English (United Kingdom):English:en_GB D/SpeakRepeatedly( 4837): English (United States):English:en_US D/SpeakRepeatedly( 4837): English (United States,Computer):English:en_US_POSIX D/SpeakRepeatedly( 4837): Spanish (Spain):Spanish:es_ES D/SpeakRepeatedly( 4837): French (France):French:fr_FR D/SpeakRepeatedly( 4837): Italian (Italy):Italian:it_IT 

注意:葡萄牙语不在TTS设置UI中列出。 当我在应用程序中以编程方式select葡萄牙语时,会说葡萄牙语口音! FWIW这里是我的代码select葡萄牙语(它接受巴西和葡萄牙语区)。

 if (locale.getDisplayName().startsWith("Portuguese")) { Log.i(SPEAK_REPEATEDLY, "Setting Locale to: " + locale.toString()); tts.setLanguage(locale); } } 

由于不同的TTS引擎为isLanguageAvailable返回不同的结果,我发现以下解决scheme在几个常见的TTS引擎上效果最好。

另请注意,从Android Lollipop开始, TextToSpeech有一个名为getAvailableLanguages的简单方法,可以轻松为您(如果设备运行API 21或更高版本)执行此操作。

您需要在分配给TextToSpeech对象的OnInitListeneronInit方法中调用以下方法。

 ArrayList<Locale> languages; TextToSpeech initTTS; private void initSupportedLanguagesLollipop() { Set<Locale> availableLocales = initTTS.getAvailableLanguages(); for (Locale locale : availableLocales) { languages.add(locale); } } private void initSupportedLanguagesLegacy() { Locale[] allLocales = Locale.getAvailableLocales(); for (Locale locale : allLocales) { try { int res = initTTS.isLanguageAvailable(locale); boolean hasVariant = (null != locale.getVariant() && locale.getVariant().length() > 0); boolean hasCountry = (null != locale.getCountry() && locale.getCountry().length() > 0); boolean isLocaleSupported = false == hasVariant && false == hasCountry && res == TextToSpeech.LANG_AVAILABLE || false == hasVariant && true == hasCountry && res == TextToSpeech.LANG_COUNTRY_AVAILABLE || res == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE; Log.d(TAG, "TextToSpeech Engine isLanguageAvailable " + locale + " (supported=" + isLocaleSupported + ",res=" + res + ", country=" + locale.getCountry() + ", variant=" + locale.getVariant() + ")"); if (true == isLocaleSupported) { languages.add(locale); } } catch (Exception ex) { Log.e(TAG, "Error checking if language is available for TTS (locale=" + locale +"): " + ex.getClass().getSimpleName() + "-" + ex.getMessage()); } } } 

从Android 5.0(API级别21)开始,已添加TextToSpeech.getAvailableLanguages以获取由TTS引擎支持的所有语言环境的集合。

 TextToSpeech tts; // assume this is initialized tts.getAvailableLanguages(); // returns a set of available locales 

我还注意到TextToSpeech.getAvailableLanguages返回的语言环境集可能不是Locale.getAvailableLocales集,也就是说,TTS引擎支持的语言环境可能不受系统支持。