audio input and output (Android in code)
-
28-10-2019 - |
Question
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
Solution
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);
}
}
}
OTHER TIPS
I set RecognizerIntent language to Turkish like that:
language = "tr-TR"; Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language); intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES, language); intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,language); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, language); intent.putExtra(RecognizerIntent.EXTRA_RESULTS, language); startActivityForResult(intent, REQUEST_CODE);
If you want to specify a language recognition you need to put:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, language);
where language
is a String with the Locale format.