为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

函数式编程 2021-09-26 14:32:35
我在我的程序语音识别器中有,当我直接在 onCreate 方法中运行 startListening 方法时,语音识别器似乎不起作用。但是当我在按钮 onClick 方法中放置 startListening 方法时,语音识别器正在工作。我想直接调用语音识别器方法 startListening,如下面的程序所示。以下是我的程序。       SpeechRecognizer mSpeechRecognizer;Intent mSpeechRecognizerIntent;TextView textView;String speakId = "one";String oldId = "";@Overrideprotected 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 方法中时不起作用。请告诉一个解决方案。
查看完整描述

1 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

我刚刚在 createSpeechRecognizer 之后设置了 used 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) {


        }



};



查看完整回答
反对 回复 2021-09-26
  • 1 回答
  • 0 关注
  • 197 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信