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

发布于 2024-10-15 18:59:30 字数 246 浏览 2 评论 0原文

我正在研究抽认卡程序的文本转语音实现。应读出不同语言的文本。为了正确执行此操作,用户必须选择要阅读的文本的语言(毫无疑问将被存储并稍后使用)。

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


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

I'm working on a text-to-speech implementation of a flashcard program. Text in different languages should be read out. In order to do this properly the user has to select the language of the text to read (will be stored and used later without question).

Is there a possibility of getting the available TTS languages on an Android system? If not, is there a possibility of getting all availably locales on the system?


I guess, I got it: getAvailableLocales() and tts.isLocaleAvailable(locale)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

原来分手还会想你 2024-10-22 18:59:30

其他人已经完成了艰苦的工作,位于 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 设置 UI 中未列出葡萄牙语。当我在应用程序中以编程方式选择葡萄牙语时,它会带有葡萄牙口音! FWIW 这是我选择葡萄牙语的代码(它接受巴西和葡萄牙语言环境)。

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

Someone else has done the hard work, at http://kaviddiss.com/2012/08/12/android-text-to-speech-languages/

To save you time, here's their code extract

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

The results depend on which TTS engine has been selected. For instance, one of my phones includes both the Pico-TTS and Google-text-to-speech engines.

Q-Smart (Vietnamese Phone with Google TTS as selected engine)

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

And with Pico selected

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

Note:
Portuguese isn’t listed in the TTS Settings UI. When I select Portuguese programmatically in my app it speaks with a Portuguese accent! FWIW here's my code to select Portuguese (it accepts both Brazilian and Portuguese locales).

if (locale.getDisplayName().startsWith("Portuguese")) {
    Log.i(SPEAK_REPEATEDLY, "Setting Locale to: " + locale.toString());
    tts.setLanguage(locale);
    }
}
烟凡古楼 2024-10-22 18:59:30

由于不同的 TTS 引擎返回的 isLanguageAvailable 结果不同,我发现以下解决方案在几种常见的 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());
        }
    }
}

Since different TTS engines return different results for isLanguageAvailable, I found that the following solution works best on several common TTS engines.

Please also note that starting with Android Lollipop, there is a simple method in TextToSpeech called getAvailableLanguages that does that easily for you (if the device is running API 21 or later).

You need to call the following methods in the onInit method of your OnInitListener assigned to the TextToSpeech object.

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());
        }
    }
}
黎夕旧梦 2024-10-22 18:59:30

使用以下函数查找设备上所有可用的 TTS 区域设置。

Locale.getAvailableLocales()

输出: Arrays.toString(Locale.getAvailableLocales())

[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]

Find all available TTS Locale on the device using following function.

Locale.getAvailableLocales()

Output of: Arrays.toString(Locale.getAvailableLocales())

[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]
牛↙奶布丁 2024-10-22 18:59:30

从 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 引擎可能支持一个区域设置系统不支持。

Starting from Android 5.0 (API level 21), TextToSpeech.getAvailableLanguages has been added to fetch a set of all locales supported by the TTS engine.

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

I have also noticed that the set of locales returned by TextToSpeech.getAvailableLanguages might not be a strict subset of Locale.getAvailableLocales, i.e. there might a locale supported by the TTS engine that isn't supported by the system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文