正常调用时,除了 onClick 等按钮事件之外,语音识别器无法工作

问题描述 投票:0回答:2

我的程序中有语音识别器,当我直接在 onCreate 方法中运行 startListening 方法时,语音识别器似乎不起作用。 但是当我将 startListening 方法放在按钮 onClick 方法中时,语音识别器正在工作。我希望直接调用语音识别器方法 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 方法中时不起作用。 请告知解决方案。

java android speech-recognition
2个回答
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) {

        }


};

0
投票

已取得 root 权限的设备的行为可能有所不同,请检查未取得 root 权限的设备。

对于未root的设备

onRmsChanged 可能会抛出错误, 所以在RecognitionListener的所有方法中至少添加一个Log。

© www.soinside.com 2019 - 2024. All rights reserved.