set Hindi as TextToSpeech language in android programmatically

☆樱花仙子☆ 提交于 2021-01-24 12:05:09

问题


Unable to set hindi as speech language of texttospeech even though My mobile tts engine fully supports hindi textospeech

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

回答1:


Firstly, as a test, set your text to speech engine to Hindi in the Android Text to Speech Settings. Without having to apply any changes to your code, it should successfully speak in Hindi. If it doesn't, check the log output to see if there is a problem.

Next, your code is reliant on isLanguageAvailable() returning TextToSpeech.LANG_AVAILABLE, if it doesn't you will not attempt to set the Locale. There are many other possible responses. Check the log output of the two case switch statements below.

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

Please be aware, the responses to isLanguageAvailable() are notoriously unreliable. See my edit on the answer here as to why.

Finally, make sure you are setting the language of the TTS object after onInit() has returned SUCCESS




回答2:


Try:

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

I hope it will work if it was not till now.




回答3:


try this..

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

instead of

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

I hope it will work.




回答4:


After creating an object of TextToSpeech class, you need to configure it (or check it's available state/values) into TextToSpeech.OnInitListener's onInit() callback. You will get reliable information there about your TextToSpeech object.

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
}

Logs I got for above onInit callback:

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


来源:https://stackoverflow.com/questions/37790158/set-hindi-as-texttospeech-language-in-android-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!