除onClick之类的按钮事件外,语音识别器通常无法正常运行

时间:2018-09-17 11:35:45

标签: java android speech-recognition

我的程序中有语音识别器,当我直接在onCreate方法中运行startListening方法时,语音识别器似乎不起作用。 但是,当我在按钮onClick方法中放置startListening方法时,语音识别器就会工作。我希望语音识别器方法startListening直接调用,如下面的程序所示。

以下是我的程序。

       SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
TextView textView;
String speakId = "one";
String oldId = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_robo);
    textView = (TextView) findViewById(R.id.textView);

    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);


    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
            Locale.getDefault());


    mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
        @Override
        public void onReadyForSpeech(Bundle params) {

        }

        @Override
        public void onBeginningOfSpeech() {

        }

        @Override
        public void onRmsChanged(float rmsdB) {

        }

        @Override
        public void onBufferReceived(byte[] buffer) {

        }

        @Override
        public void onEndOfSpeech() {

        }

        @Override
        public void onError(int error) {

        }

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            textView.setText(matches.get(0));

            //displaying the first match
            //if (matches != null)
            //processResult(matches.get(0));
        }

        @Override
        public void onPartialResults(Bundle partialResults) {

        }

        @Override
        public void onEvent(int eventType, Bundle params) {

        }
    });

    /*
        below SpeechRecognizer does not start listening when called in MainActivity.
         */

    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);


}

}

但是当我在按钮onClick事件中调用mSpeechRecognizer.startListening(mSpeechRecognizerIntent)时,SpeechRecognizer仍在工作。

    public void Listen(View view) {
    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}

为什么将代码放在onCreate方法中而不是放在onClick方法中却不起作用。 请为此提供解决方案。

1 个答案:

答案 0 :(得分:1)

我只是在createSpeechRecognizer之后设置了使用过的setRecognitionListener,并且代码可以正常工作。

    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizer.setRecognitionListener(recognitionListener);

    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
            Locale.getDefault());

然后调用

 mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

它不使用按钮事件就可以工作。

RecogniserListener类应以这种方式声明。

    RecognitionListener recognitionListener = new RecognitionListener() {
        @Override
        public void onReadyForSpeech(Bundle params) {

        }

        @Override
        public void onBeginningOfSpeech() {

        }

        @Override
        public void onRmsChanged(float rmsdB) {

        }

        @Override
        public void onBufferReceived(byte[] buffer) {

        }

        @Override
        public void onEndOfSpeech() {

        }

        @Override
        public void onError(int error) {
            String message;
            switch (error) {
                case SpeechRecognizer.ERROR_AUDIO:
                    message = "Audio recording error";
                    break;
                case SpeechRecognizer.ERROR_CLIENT:
                    message = "Client side error";
                    break;
                case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
                    message = "Insufficient permissions";
                    break;
                case SpeechRecognizer.ERROR_NETWORK:
                    message = "Network error";
                    break;
                case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
                    message = "Network timeout";
                    break;
                case SpeechRecognizer.ERROR_NO_MATCH:
                    message = "No match";
                    break;
                case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
                    message = "RecognitionService busy";
                    break;
                case SpeechRecognizer.ERROR_SERVER:
                    message = "error from server";
                    break;
                case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
                    message = "No speech input";
                    break;
                default:
                    message = "Didn't understand, please try again.";
                    break;
            }
            textView.setText(message);
        }

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            textView.setText(matches.get(0));

            //displaying the first match
            //if (matches != null)
                //processResult(matches.get(0));
        }

        @Override
        public void onPartialResults(Bundle partialResults) {

        }

        @Override
        public void onEvent(int eventType, Bundle params) {

        }


};