Change default language for Speech recognition in my app

北城以北 提交于 2021-02-08 07:22:32

问题


I make an app in English. My app uses Speech recognition. But if I install this app on device with another system language, French or Russian for example. My speech recognition doesn't work. It works only for language which by default in system. How can I make English language for Speech recognition by default for my app?

I found this method but it doesn't work

Locale myLocale;
    myLocale = new Locale("English (US)", "en_US");
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

回答1:


You can try with this code:

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

You can also check out the complete code for that at here:https://github.com/gast-lib



来源:https://stackoverflow.com/questions/33029417/change-default-language-for-speech-recognition-in-my-app

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