在Android中以编程方式将Hindi设置为TextToSpeech语言

时间:2016-06-13 12:42:34

标签: android text-to-speech

即使My mobile tts引擎完全支持hindi textospeech,也无法将hindi设置为texttospeech的语音语言

        if(t1.isLanguageAvailable(new Locale("hi_IN"))==TextToSpeech.LANG_AVAILABLE)
            t1.setLanguage(new Locale ("hi_IN"));
        else
            Toast.makeText(getApplicationContext(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();

4 个答案:

答案 0 :(得分:0)

首先,作为测试,在Android文本到语音设置中将文本设置为语音引擎。无需对代码进行任何更改,它应该成功地用印地语说话。如果没有,请检查日志输出以查看是否存在问题。

接下来,您的代码依赖于isLanguageAvailable()返回TextToSpeech.LANG_AVAILABLE,如果不是,您将不会尝试设置区域设置。还有许多其他可能的回应。检查下面两个case switch语句的日志输出。

    final Locale loc = new Locale("hin", "IND");

    // switch(tts.isLanguageAvailable(loc)){
    switch(tts.setLanguage(loc)){

        case TextToSpeech.LANG_AVAILABLE:
            Log.i("TAG", "LANG_AVAILABLE");
            break;
        case TextToSpeech.LANG_COUNTRY_AVAILABLE:
            Log.i("TAG", "LANG_COUNTRY_AVAILABLE");
            break;
        case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
            Log.i("TAG", "LANG_COUNTRY_VAR_AVAILABLE");
            break;
        case TextToSpeech.LANG_MISSING_DATA:
            Log.i("TAG", "LANG_MISSING_DATA");
            break;
        case TextToSpeech.LANG_NOT_SUPPORTED:
            Log.i("TAG", "LANG_NOT_SUPPORTED");
            break;
    }

请注意,对isLanguageAvailable()的回复众所周知是不可靠的。有关原因,请参阅my edit on the answer here

最后,确保在onInit()返回SUCCESS之后设置TTS对象的语言

答案 1 :(得分:0)

尝试:

t1.setLanguage( new Locale( "hin", "IND", "variant" ) );

我希望它能够发挥作用,直到现在。

答案 2 :(得分:0)

试试这个..

t1.setLanguage(new Locale ("hi","IN"));

而不是

t1.setLanguage(new Locale ("hi_IN"));

我希望它能奏效。

答案 3 :(得分:0)

创建 TextToSpeech 类的对象后,您需要将其配置(或检查其可用状态/值)到 TextToSpeech.OnInitListeneronInit() 回调中。您将在那里获得有关 TextToSpeech 对象的可靠信息。

private fun initTextToSpeech(){
    tts = TextToSpeech(this,this) // TextToSpeech(context,listener)
}

override fun onInit(status: Int) {
    Log.e(TAG, "onInit: status: $status")
    if(status!=TextToSpeech.SUCCESS)return

    val locale = Locale("hi","IN")

    val isLangCountryAvailable = tts.isLanguageAvailable(locale)
    Log.e(TAG, "onInit: isLangCountryAvailable: $isLangCountryAvailable")

    if(isLangCountryAvailable==TextToSpeech.LANG_COUNTRY_AVAILABLE) {
        val setLanguageResult = tts.setLanguage(locale)
        Log.e(TAG, "onInit: setLanguageResult: $setLanguageResult")
    }

    Log.e(TAG, "onInit: availableLanguages: ${tts.availableLanguages}")
    Log.e(TAG, "onInit: voice: ${tts.voice}")

    binding.btnSpeak.isEnabled = status==TextToSpeech.SUCCESS
}

我在 onInit 回调上面得到的日志:

MainActivity: onInit: status: 0
MainActivity: onInit: isLangCountryAvailable: 1
MainActivity: onInit: setLanguageResult: 1
MainActivity: onInit: availableLanguages: [ko_KR, mr_IN, ru_RU, zh_TW, hu_HU, 
th_TH, ur_PK, nb_NO, da_DK, tr_TR, et_EE, bs, sw, pt_PT, vi_VN, en_US, sv_SE, 
ar, su_ID, bn_BD, gu_IN, kn_IN, el_GR, hi_IN, fi_FI, km_KH, bn_IN, fr_FR, 
uk_UA, en_AU, nl_NL, fr_CA, sr, pt_BR, ml_IN, si_LK, de_DE, ku, cs_CZ, pl_PL, 
sk_SK, fil_PH, it_IT, ne_NP, hr, en_NG, zh_CN, es_ES, cy, ta_IN, ja_JP, sq, 
yue_HK, en_IN, es_US, jv_ID, la, in_ID, te_IN, ro_RO, ca, en_GB]
MainActivity: onInit: voice: Voice[Name: hi-IN-language, locale: hi_IN, 
quality: 400, latency: 200, requiresNetwork: false, features: 
[networkTimeoutMs, legacySetLanguageVoice, networkRetriesCount]]