0

我正在使用以下代码来识别文本。首先,我调用对话框来选择识别语言。我将该论点传递给识别器意图。不幸的是,我的手机只能识别“en-US”(我也尝试过“fr-FR”和“ru-RU”)。有没有什么事情没有做,或者我怎样才能把清单缩小到工作语言?

代码

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

1 回答 1

0

问题是由错误的附加功能引起的。替换方法

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);
        }
于 2014-04-22T18:14:22.040 回答