23

I've been working on speech Recognition API in android and found out that the speech results vary allot when the language settings are changed , is there a way to set it programmatically ? or is there an intent to lunch the speech language settings screen ? or what else ? note: I tried to use this intent extra:

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

but it was ineffective

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, Locale.getDefault()); ..it will run if gooogle support that language.. – Zar E Ahmer May 29 '14 at 10:27
  • 1
    The extra that is supposed to serve that purpose is EXTRA_LANGUAGE, not EXTRA_LANGUAGE_PREFERENCE. However, LANGUAGE_EXTRA for some reason only works until 4.3. Starting from 4.4, Android allows multiple languages to be selected in Settings, and will always try to "guess" the language according to system settings and completely ignores the EXTRA_LANGUAGE extra. So, anybody? – matteo Sep 05 '14 at 16:38

8 Answers8

50

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

Confuse
  • 5,646
  • 7
  • 36
  • 58
gregm
  • 12,019
  • 7
  • 56
  • 78
  • 6
    I think it easier to use RecognizerIntent method for creating the intent: ` val detailsIntent = RecognizerIntent.getVoiceDetailsIntent(context) activity.sendOrderedBroadcast(detailsIntent, null, LanguageDetailsChecker({ languages.accept(it) }), null, Activity.RESULT_OK, null, null)` Intent from the sample above didn't work for me :( getVoiceDetailsIntent() did :) – Aetherna Nov 20 '17 at 15:04
  • 3
    Putting "en-US" doesn't work for me. Use `intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US.toString());` as suggested in the answer by @orina1123 – M3RS Jul 16 '18 at 13:20
  • 1
    RecognizerIntent.getVoiceDetailsIntent(context) is the right way. I was getting null without it on Android Q. – ashishb Feb 25 '20 at 07:56
  • You need to set `detailsIntent.setPackage("com.google.android.googlequicksearchbox");` for this to work, see https://stackoverflow.com/a/48653201/ – user000001 Jul 10 '21 at 10:08
13

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.

orina1123
  • 131
  • 1
  • 2
  • Thanks, man. This saved hours of the time of mine!!! The problem was similar but little difference. Can find it here https://stackoverflow.com/questions/51048466/google-speech-to-text-extra-language-set-to-kn-but-not-working – Varun A M Jun 27 '18 at 07:05
12

there is no solution but a hackaround...

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

check here the complete story.

Arnav M.
  • 2,590
  • 1
  • 27
  • 44
8

Have you tried this:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
Pargat
  • 769
  • 9
  • 23
6

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.

kwishnu
  • 1,730
  • 19
  • 17
  • 4
    Did you REALLY put all those into the intent that you pass to startListening?? Did it really work? And didn't it with EXTRA_LANGUAGE alone? What Android versions did you test? EXTRA_LANGUAGE is the only one that is supposed to be relevant here. The other two, according to documentation, are meant for querying the system for supported language, NOT for recognition. So they should be totally meaningless here. For me, it works with EXTRA_LANGUAGE alone until 4.3, then the extra is completely ignored starting from 4.4. – matteo Sep 05 '14 at 16:42
  • I'm not sure whether the api has changed but `EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE` takes a boolean. – Srikar Reddy Nov 29 '20 at 17:57
  • which country supported for those languages anyway within this library? – gumuruh Mar 29 '22 at 14:51
2

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());
Oleg SH
  • 443
  • 5
  • 11
2

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);
dileep krishnan
  • 326
  • 4
  • 7
0

I used this code:

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

Hope you can run your app now.

DirtyBit
  • 16,613
  • 4
  • 34
  • 55