如何在android上的语音识别中设置语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10538791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:05:56  来源:igfitidea点击:

How to set the language in speech recognition on android?

androidspeech-recognition

提问by Mr.Me

I've been working on speech Recognition API in android and found out that the speech results vary allot when the language settings are changed , is there a way to set it programmatically ? or is there an intent to lunch the speech language settings screen ? or what else ? note: I tried to use this intent extra:

我一直在研究 android 中的语音识别 API,发现更改语言设置时语音结果会有所不同,有没有办法以编程方式设置它?或者是否打算在语音语言设置屏幕上吃午饭?或者还有什么?注意:我试图额外使用这个意图:

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

but it was ineffective

但它是无效的

回答by gregm

As pargat says, this will do it:

正如 pargat 所说,这会做到:

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_DETAILSordered broadcast like so:

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

where LanguageDetailsChecker is something like this:

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

For the complete code check out this github project: https://github.com/gast-lib

有关完整代码,请查看此 github 项目:https: //github.com/gast-lib

回答by Arnav M.

there is no solution but a hackaround...

没有解决方案,只有一个hackaround......

intent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{"en"});

check herethe complete story.

在这里查看完整的故事。

回答by orina1123

This will work:

这将起作用:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en_US");

You have to use "en_US" instead of "en-US". The former is the right format of Java locale tag.

您必须使用“en_US”而不是“en-US”。前者是 Java 语言环境标记的正确格式。

It is suggested that you use

建议您使用

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US.toString());

to avoid remembering such detail.

避免记住这样的细节。

回答by Pargat

Have you tried this:

你有没有试过这个:

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

回答by kwishnu

I finally got my app to restrict voice recognition results to a specified language input (handing it, e.g., "ja" for Japanese or "fr" for French) by adding all 3 of the following extras:

通过添加以下所有 3 个附加功能,我终于让我的应用程序将语音识别结果限制为指定的语言输入(处理它,例如,日语的“ja”或法语的“fr”):

String languagePref = "de";//or, whatever iso code...
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, languagePref);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, languagePref); 
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, languagePref);

Hope this helps someone.

希望这可以帮助某人。

回答by Oleg SH

I tried to use

我试着用

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

but it did not work for me (did not take the system language). Helped here like this:

但它对我不起作用(没有采用系统语言)。像这样在这里帮助:

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString());

回答by dileep krishnan

this code is to set the language in speech recognization

此代码用于设置语音识别中的语言

  String languagePref = "te-IN";//this is for telugu

     //kannada --->  "kn-IN"
     //tamil--->  "ta-IN".....

            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, languagePref);

回答by user5278060

I used this code:

我使用了这个代码:

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

Hope you can run your app now.

希望您现在可以运行您的应用程序。