android语音识别停止

时间:2016-02-29 22:19:21

标签: listener voice-recognition recognizer-intent

是否可以停止倾听用户语音的意图? 例如,我有这个听众:

  @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es");
                        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
                        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
                        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
                        return true;
                    case MotionEvent.ACTION_UP:
                        //Code to stop listening user speech

                        return true; 
            }

我的想法是,用户必须按住特定按钮,以便应用程序听取语音,例如Whatsapp中的麦克风按钮。

修改

我想我已经尝试过@brandall告诉我的事情了。以下是代码的修改:

  public boolean onTouch(View v, MotionEvent event) {
                    SpeechRecognizer speechRecognizer = createSpeechRecognizer(context);
                    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    switch(event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "es");
                            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
                            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
                            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
                            speechRecognizer.startListening(intent);
                            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
                            return true; 
                        case MotionEvent.ACTION_UP:
                            speechRecognizer.stopListening();
                            return true;
                    }
                    return false;
                }

            });

0 个答案:

没有答案