Ersin Gülbahar Ersin Gülbahar - 5 months ago 18
Android Question

audio input and output (Android in code)

How i change audio input and output language in my android code? Should i edit this code to change audio input output language ? {I want to set one variable to En to speech language English or set variable to Tr to select Turkish if this possible?}

public Intent getRecognizeIntent()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
return intent;
}


Or this help me to change? -> EXTRA_LANGUAGE_PREFERENCE

Answer

This should work in most cases:

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

or

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

Where you pick the locale you want and pass it's toString() to EXTRA_LANGUAGE_PREFERENCE

However, to be correct, you should try doing a language check, and observing the returned "voices". Manually or automatically, pick the "voice" string you want and then use that for the RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE value.

Here is some code to execute the language check:

public static void getLanguageDetails(Context context,
        OnLanguageDetailsListener andThen)
{
    Intent detailsIntent = new Intent(
            RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
    LanguageDetailsChecker checker = new LanguageDetailsChecker(andThen);
    context.sendOrderedBroadcast(detailsIntent, null, checker, 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);
        }
        }
}
Comments