How to set the language in speech recognition on android?

前端 未结 8 1226
清歌不尽
清歌不尽 2020-11-28 06:00

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 pr

相关标签:
8条回答
  • 2020-11-28 06:41

    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.

    It is suggested that you use

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

    to avoid remembering such detail.

    0 讨论(0)
  • 2020-11-28 06:47

    As pargat says, this will do it:

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

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

    0 讨论(0)
  • 2020-11-28 06:47

    Have you tried this:

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
    
    0 讨论(0)
  • 2020-11-28 06:50

    I used this code:

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

    Hope you can run your app now.

    0 讨论(0)
  • 2020-11-28 07:03

    there is no solution but a hackaround...

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

    check here the complete story.

    0 讨论(0)
  • 2020-11-28 07:03

    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());
    
    0 讨论(0)
提交回复
热议问题