How to set the language in speech recognition on android?

怎甘沉沦 提交于 2019-11-26 21:57:15
gregm

As pargat says, this will do it:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

Also, your app can query for the list of supported languages by sending a RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS ordered broadcast like so:

    Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    sendOrderedBroadcast(
            detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);

where LanguageDetailsChecker is something like this:

public class LanguageDetailsChecker extends BroadcastReceiver
{
    private List<String> supportedLanguages;

    private String languagePreference;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle results = getResultExtras(true);
        if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
        {
            languagePreference =
                    results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
        }
        if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
        {
            supportedLanguages =
                    results.getStringArrayList(
                            RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
        }
    }
}

For the complete code check out this github project: https://github.com/gast-lib

there is no solution but a hackaround...

intent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{"en"});

check here the complete story.

This will work:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en_US");

You have to use "en_US" instead of "en-US". The former is the right format of Java locale tag.

It is suggested that you use

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US.toString());

to avoid remembering such detail.

Have you tried this:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");

I finally got my app to restrict voice recognition results to a specified language input (handing it, e.g., "ja" for Japanese or "fr" for French) by adding all 3 of the following extras:

String languagePref = "de";//or, whatever iso code...
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, languagePref);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, languagePref); 
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, languagePref);

Hope this helps someone.

I tried to use

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

but it did not work for me (did not take the system language). Helped here like this:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());

this code is to set the language in speech recognization

  String languagePref = "te-IN";//this is for telugu

     //kannada --->  "kn-IN"
     //tamil--->  "ta-IN".....

            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, languagePref);
user5278060

I used this code:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

Hope you can run your app now.

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