Android SpeechRecognizer能识别英语吗?

时间:2014-04-22 17:41:49

标签: android android-speech-api

我使用以下代码识别文字。首先,我调用对话框来选择识别语言。我将该参数传递给RecognizerIntent。不幸的是,只有" en-US"在我的手机上被识别出来(我也试过了#34; fr-FR"和#34; 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);
        }
    }

1 个答案:

答案 0 :(得分: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);
        }