在我的应用程序中更改语音识别的默认语言

Joh*_*Pix 2 android speech-recognition text-to-speech speech-to-text

我用英语制作了一个应用程序。我的应用程序使用语音识别。但是,如果我在使用其他系统语言(例如法语或俄语)的设备上安装此应用程序。我的语音识别不起作用。它仅适用于系统默认的语言。如何为我的应用程序默认使用英语进行语音识别?

我找到了这个方法,但它不起作用

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());
Run Code Online (Sandbox Code Playgroud)

Raj*_*sar 5

您可以尝试使用以下代码:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
Run Code Online (Sandbox Code Playgroud)

此外,您的应用程序可以通过发送 RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS 有序广播来查询支持的语言列表,如下所示:

 Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
sendOrderedBroadcast(
        detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);
Run Code Online (Sandbox Code Playgroud)

其中 LanguageDetailsChecker 是这样的:

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

你也可以在这里查看完整的代码:https://github.com/gast-lib