[Android] TextToSpeech

Android 從 API 21 (Lollipop) 之後開始支援 TextToSpeech 功能,也就是說輸入一個字串之後,Android 裝置可以自動讀出這個字串,是一個很強的的功能,本篇要來探討如何在程式導入這個功能?

首先我們先展示如何嵌入 TextToSpeech 到一般的 Activity 中:

public class TestActivity extends Activity{
    
    private TextToSpeech mTTs;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        final Locale[] locales = Locale.getAvailableLocales();
        initTTS();
    }
}

緊接著我們展示如何初始化 TextToSpeech 並且以 API 23  為例展示目前可以支援 TextToSpeech 的語言列表:

private void initTTS(final Locale[] locales){
    if( mTTs == null ){
        mTTs = new TextToSpeech(this, new OnInitListener(){
            @Override
            public void onInit(int arg0){
                if( arg0 == TextToSpeech.SUCCESS ){
                    List<Locale> localeList = new ArrayList<>();
                    for (Locale locale : locales) {
                        int res = mTTS.isLanguageAvailable(locale);
                        if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                            localeList.add(locale);
                        }
                    }
                }
            }
        });    
    }
}

 

程式執行結果:我們可以發現相對於裝置的在地化模組,TextToSpeech 可以使用的語言數相對較少,只有少少的 42 個!
localeList = [bn_BD, bn_IN, cs_CZ, da_DK, de_DE, el_GR, en_AU, en_GB, en_IN, en_US, en_US_POSIX, es_ES, es_US, et_EE, fi_FI, fil_PH, fr_CA, fr_FR, hi_IN, hu_HU, in_ID, it_IT, ja_JP, km_KH, ko_KR, nb_NO, ne_NP, nl_NL, pl_PL, pt_BR, pt_PT, ro_RO, ru_RU, si_LK, sk_SK, sv_SE, th_TH, tr_TR, uk_UA, vi_VN, zh_CN_#Hans, zh_TW_#Hant]

locales = [af, af_NA, af_ZA, agq, agq_CM, ak, ak_GH, am, am_ET, ar, ar_001, ar_AE, ar_BH, ar_DJ, ar_DZ, ar_EG, ar_EH, ar_ER, ar_IL, ar_IQ, ar_JO, ar_KM, ar_KW, ar_LB, ar_LY, ar_MA, ar_MR, ar_OM, ar_PS, ar_QA, ar_SA, ar_SD, ar_SO, ar_SS, ar_SY, ar_TD, ar_TN, ar_YE, as, as_IN, asa, asa_TZ, az, az__#Cyrl, az_AZ_#Cyrl, az__#Latn, az_AZ_#Latn, bas, bas_CM, be, be_BY, bem, bem_ZM, bez, bez_TZ, bg, bg_BG, bm, bm__#Latn, bm_ML_#Latn, bn, bn_BD, bn_IN, bo, bo_CN, bo_IN, br, br_FR, brx, brx_IN, bs, bs__#Cyrl, bs_BA_#Cyrl, bs__#Latn, bs_BA_#Latn, ca, ca_AD, ca_ES, ca_FR, ca_IT, cgg, cgg_UG, chr, chr_US, cs, cs_CZ, cy, cy_GB, da, da_DK, da_GL, dav, dav_KE, de, de_AT, de_BE, de_CH, de_DE, de_LI, de_LU, dje, dje_NE, dsb, dsb_DE, dua, dua_CM, dyo, dyo_SN, dz, dz_BT, ebu, ebu_KE, ee, ee_GH, ee_TG, el, el_CY, el_GR, en, en_001, en_150, en_AG, en_AI, en_AS, en_AU, en_BB, en_BE, en_BM, en_BS, en_BW, en_BZ, en_CA, en_CC, en_CK, en_CM, en_CX, en_DG, en_DM, en_ER, en_FJ, en_FK, en_FM, en_GB, en_GD, en_GG, en_GH, en_GI, en_GM, en_GU, en_GY, en_HK, en_IE, en_IM, en_IN, en_IO, en_JE, en_JM, en_KE, en_KI, en_KN, en_KY, en_LC, en_LR, en_LS, en_MG, en_MH, en_MO, en_MP, en_MS, en_MT, en_MU, en_MW, en_MY, en_NA, en_NF, en_NG, en_NR, en_NU, en_NZ, en_PG, en_PH, en_PK, en_PN, en_PR, en_PW, en_RW, en_SB, en_SC, en_SD, en_SG, en_SH, en_SL, en_SS, en_SX, en_SZ, en_TC, en_TK, en_TO, en_TT, en_TV, en_TZ, en_UG, en_UM, en_US, en_US_POSIX, en_VC, en_VG, en_VI, en_VU, en_WS, en_ZA, en_ZM, en_ZW, eo, es, es_419, es_AR, es_BO, es_CL, es_CO, es_CR, es_CU, es_DO, es_EA, es_EC, es_ES, es_GQ, es_GT, es_HN, es_IC, es_MX, es_NI, es_PA, es_PE, es_PH, es_PR, es_PY, es_SV, es_US, es_UY, es_VE, et, et_EE, eu, eu_ES, ewo, ewo_CM, fa, fa_AF, fa_IR, ff, ff_CM, ff_GN, ff_MR, ff_SN, fi, fi_FI, fil, fil_PH, fo, fo_FO, fr, fr_BE, fr_BF, fr_BI, fr_BJ, fr_BL, fr_CA, fr_CD, fr_CF, fr_CG, fr_CH, fr_CI, fr_CM, fr_DJ, fr_DZ, fr_FR, fr_GA, fr_GF, fr_GN, fr_GP, fr_GQ, fr_HT, fr_KM, fr_LU, fr_MA, fr_MC, fr_MF, fr_MG, fr_ML, fr_MQ, fr_MR, fr_MU, fr_NC, fr_NE, fr_PF, fr_PM, fr_RE, fr_RW, fr_SC, fr_SN, fr_SY, fr_TD, fr_TG, fr_TN, fr_VU, fr_WF, fr_YT, fur, fur_IT, fy, fy_NL, ga, ga_IE, gd, gd_GB, gl, gl_ES, gsw, gsw_CH, gsw_FR, gsw_LI, gu, gu_IN, guz, guz_KE, gv, gv_IM, ha, ha__#Latn, ha_GH_#Latn, ha_NE_#Latn, ha_NG_#Latn, haw, haw_US, iw, iw_IL, hi, hi_IN, hr, hr_BA, hr_HR, hsb, hsb_DE, hu, hu_HU, hy, hy_AM, in, in_ID, ig, ig_NG, ii, ii_CN, is, is_IS, it, it_CH, it_IT, it_SM, ja, ja_JP, jgo, jgo_CM, jmc, jmc_TZ, ka, ka_GE, kab, kab_DZ, kam, kam_KE, kde, kde_TZ, kea, kea_CV, khq, khq_ML, ki, ki_KE, kk, kk__#Cyrl, kk_KZ_#Cyrl, kkj, kkj_CM, kl, kl_GL, kln, kln_KE, km, km_KH, kn, kn_IN, ko, ko_KP, ko_KR, kok, kok_IN, ks, ks__#Arab, ks_IN_#Arab, ksb, ksb_TZ, ksf, ksf_CM, ksh, ksh_DE, kw, kw_GB, ky, ky__#Cyrl, ky_KG_#Cyrl, lag, lag_TZ, lb, lb_LU, lg, lg_UG, lkt, lkt_US, ln, ln_AO, ln_CD, ln_CF, ln_CG, lo, lo_LA, lt, lt_LT, lu, lu_CD, luo, luo_KE, luy, luy_KE, lv, lv_LV, mas, mas_KE, mas_TZ, mer, mer_KE, mfe, mfe_MU, mg, mg_MG, mgh, mgh_MZ, mgo, mgo_CM, mk, mk_MK, ml, ml_IN, mn, mn__#Cyrl, mn_MN_#Cyrl, mr, mr_IN, ms, ms__#Latn, ms_BN_#Latn, ms_MY_#Latn, ms_SG_#Latn, mt, mt_MT, mua, mua_CM, my, my_MM, naq, naq_NA, nb, nb_NO, nb_SJ, nd, nd_ZW, ne, ne_IN, ne_NP, nl, nl_AW, nl_BE, nl_BQ, nl_CW, nl_NL, nl_SR, nl_SX, nmg, nmg_CM, nn, nn_NO, nnh, nnh_CM, nus, nus_SD, nyn, nyn_UG, om, om_ET, om_KE, or, or_IN, os, os_GE, os_RU, pa, pa__#Arab, pa_PK_#Arab, pa__#Guru, pa_IN_#Guru, pl, pl_PL, ps, ps_AF, pt, pt_AO, pt_BR, pt_CV, pt_GW, pt_MO, pt_MZ, pt_PT, pt_ST, pt_TL, qu, qu_BO, qu_EC, qu_PE, rm, rm_CH, rn, rn_BI, ro, ro_MD, ro_RO, rof, rof_TZ, ru, ru_BY, ru_KG, ru_KZ, ru_MD, ru_RU, ru_UA, rw, rw_RW, rwk, rwk_TZ, sah, sah_RU, saq, saq_KE, sbp, sbp_TZ, se, se_FI, se_NO, se_SE, seh, seh_MZ, ses, ses_ML, sg, sg_CF, shi, shi__#Latn, shi_MA_#Latn, shi__#Tfng, shi_MA_#Tfng, si, si_LK, sk, sk_SK, sl, sl_SI, smn, smn_FI, sn, sn_ZW, so, so_DJ, so_ET, so_KE, so_SO, sq, sq_AL, sq_MK, sq_XK, sr, sr__#Cyrl, sr_BA_#Cyrl, sr_ME_#Cyrl, sr_RS_#Cyrl, sr_XK_#Cyrl, sr__#Latn, sr_BA_#Latn, sr_ME_#Latn, sr_RS_#Latn, sr_XK_#Latn, sv, sv_AX, sv_FI, sv_SE, sw, sw_CD, sw_KE, sw_TZ, sw_UG, ta, ta_IN, ta_LK, ta_MY, ta_SG, te, te_IN, teo, teo_KE, teo_UG, th, th_TH, ti, ti_ER, ti_ET, to, to_TO, tr, tr_CY, tr_TR, twq, twq_NE, tzm, tzm__#Latn, tzm_MA_#Latn, ug, ug__#Arab, ug_CN_#Arab, uk, uk_UA, ur, ur_IN, ur_PK, uz, uz__#Arab, uz_AF_#Arab, uz__#Cyrl, uz_UZ_#Cyrl, uz__#Latn, uz_UZ_#Latn, vai, vai__#Latn, vai_LR_#Latn, vai__#Vaii, vai_LR_#Vaii, vi, vi_VN, vun, vun_TZ, wae, wae_CH, xog, xog_UG, yav, yav_CM, ji, ji_001, yo, yo_BJ, yo_NG, zgh, zgh_MA, zh, zh__#Hans, zh_CN_#Hans, zh_HK_#Hans, zh_MO_#Hans, zh_SG_#Hans, zh__#Hant, zh_HK_#Hant, zh_MO_#Hant, zh_TW_#Hant, zu, zu_ZA, zy, zy_MM]

當選定語言之後,需要透過以下方式設定 TextToSpeech 的 Engine,以下以指定美式英文為例:

Locale l = Locale.US;
if( tts.isLanguageAvailable( l ) == TextToSpeech.LANG_COUNTRY_AVAILABLE ){
    tts.setLanguage( l );
}