【问题标题】:Change default language for Speech recognition in my app在我的应用中更改语音识别的默认语言
【发布时间】:2016-01-06 21:07:15
【问题描述】:

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

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

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

【问题讨论】:

  • 你想在Android语音识别中设置语言吗?
  • 是的,默认情况下只为我的应用程序设置英语。有可能吗?

标签: android speech-recognition text-to-speech speech-to-text


【解决方案1】:

你可以试试这段代码:

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

此外,您的应用可以通过发送 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);

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

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

【讨论】:

  • 谢谢!我刚刚将 Intent 从 Locale.ENGLISH 更改为“en-US”,它开始起作用了!谢谢你的链接!
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多