Question

I am using following code to recognize text. At first I invoke the dialog to choose language for recognition. That I pass that argument to RecognizerIntent. Unfortunatly, only "en-US" is recognized on my phone(I also tried "fr-FR" and "ru-RU"). Is there something have not done or how can I narrow list to working languages?

Code

    public void recognize(View v) {
            Intent detailsIntent = new Intent(
                    RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
            sendOrderedBroadcast(detailsIntent, null, new LanguageDetailsChecker(
                    context), null, Activity.RESULT_OK, null, null);

        }
private class LanguageDetailsChecker extends BroadcastReceiver {
        Context contextApp;

        public LanguageDetailsChecker(Context context) {
            this.contextApp = context;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle results = getResultExtras(true);
            ArrayList<String> languages = new ArrayList<String>();
            if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
                languagePreference = results
                        .getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
            }
            if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
                languages = results
                        .getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
            }

            supportedLanguages = new String[languages.size()];
            supportedLanguages = languages.toArray(supportedLanguages);
            AlertDialog.Builder b = new Builder(contextApp);
            for (String s : supportedLanguages) {
                Log.d("Supported languages", s);
            }
            b.setTitle("Choose your language");
            b.setItems(supportedLanguages, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    chooseLanguage(which);

                }

            });

            b.show();
        }

        private void chooseLanguage(int i) {
            int which = i;
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    supportedLanguages[which]);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
                    supportedLanguages[which]);
            intent.putExtra(
                    RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,
                    supportedLanguages[which]);
            Log.d("Languages choosen", supportedLanguages[which]);
            startActivityForResult(intent, 300);
        }
    }
Was it helpful?

Solution

Proble was cause by wrong extras. Replace method

private void chooseLanguage(int i) {
            int which = i;
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    supportedLanguages[which]);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
                    supportedLanguages[which]);
            Log.d("Languages choosen", supportedLanguages[which]);
            startActivityForResult(intent, 300);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top