除了 onClick 等按钮事件之外,语音识别器在正常调用时不起作用

我在我的程序语音识别器中有,当我直接在 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));

        }


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


    public void Listen(View view) {

    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

}

为什么当代码放在 onCreate 方法中而不是在 onClick 方法中时不起作用。请告诉一个解决方案。


函数式编程
浏览 198回答 1
1回答

qq_笑_17

我刚刚在 createSpeechRecognizer 之后设置了 used setRecognitionListener 并且代码正在运行。&nbsp; &nbsp; mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);&nbsp; &nbsp; mSpeechRecognizer.setRecognitionListener(recognitionListener);&nbsp; &nbsp; mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);&nbsp; &nbsp; mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);&nbsp; &nbsp; mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Locale.getDefault());然后叫&nbsp;mSpeechRecognizer.startListening(mSpeechRecognizerIntent);它无需使用按钮事件即可工作。RecogniserListener 类应该以这种方式声明。&nbsp; &nbsp; RecognitionListener recognitionListener = new RecognitionListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onReadyForSpeech(Bundle params) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onBeginningOfSpeech() {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onRmsChanged(float rmsdB) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onBufferReceived(byte[] buffer) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onEndOfSpeech() {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onError(int error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_AUDIO:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Audio recording error";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_CLIENT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Client side error";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Insufficient permissions";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_NETWORK:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Network error";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Network timeout";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_NO_MATCH:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "No match";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "RecognitionService busy";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_SERVER:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "error from server";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "No speech input";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message = "Didn't understand, please try again.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setText(message);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResults(Bundle results) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setText(matches.get(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //displaying the first match&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (matches != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //processResult(matches.get(0));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onPartialResults(Bundle partialResults) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onEvent(int eventType, Bundle params) {&nbsp; &nbsp; &nbsp; &nbsp; }};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python