获取文本到语音的可用区域设置(TTS)

时间:2011-02-02 09:32:00

标签: android locale text-to-speech

我正在研究一个flashcard程序的文本到语音的实现。应该读出不同语言的文本。为了正确地做到这一点,用户必须选择要阅读的文本的语言(将在以后存储和使用,毫无疑问)。

是否有可能在Android系统上获取可用的TTS语言?如果没有,是否有可能在系统上获得所有可用的区域设置?


我想,我得到了它:getAvailableLocales()tts.isLocaleAvailable(locale)

4 个答案:

答案 0 :(得分:18)

其他人在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

结果取决于选择了哪个TTS引擎。例如,我的一部手机包括Pico-TTS和Google文字转语音引擎。

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

选择Pico

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设置界面中。当我在我的应用程序中以编程方式选择葡萄牙语时,它会说出葡萄牙语口音! FWIW这里是我选择葡萄牙语的代码(它接受巴西和葡萄牙语区域​​设置)。

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

答案 1 :(得分:12)

您可以使用以下功能找到设备的所有可用区域设置。

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]

答案 2 :(得分:10)

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

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

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

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());
        }
    }
}

答案 3 :(得分:2)

从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引擎支持的语言环境可能不受系统支持