Android Speech Recognizer not connected to the recognition service
Asked Answered
C

1

3

I want to introduce permanent voice recognition feature in my Android application.

I am aware with the fact that voice recognition freezes from time to time on Android 4.1.1 and 4.2 and because of that I built up a timer and from time to time I check if the voice recognition is still alive or not, and if it's not alive I stop it and then start it. Unfortunately at some point when it tries to restart the voice recognizer, I get something like this:

SpeechRecognizer not connected to the recognition service

and in onError callback I receive error 8 (documentation says: ERROR_RECOGNIZER_BUSY) even though I stop every timer before starting voice recognizer.

The code looks like this:

public class MainActivity extends Activity {
private SpeechRecognizer mSpeechRecognizer;
private RecognitionListener mRecognitionListener;

private Intent mi;
private boolean isSpeechRecognizerAlive;
Timer myTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mi = new Intent(getApplicationContext(), MyService.class);

    mRecognitionListener = new RecognitionListener(){

        @Override
        public void onRmsChanged(float rmsdB) {
        }

        @Override
        public void onResults(Bundle results) {
            Log.e("recognizer listener", "onResults");

            ArrayList<String> result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            for(int i=0;i<result.size();i++){
                Log.e(String.valueOf(i), result.get(i));
            }

            startRecognition(new View(getApplicationContext()));
        }

        @Override
        public void onReadyForSpeech(Bundle params) {
            Log.e("recognizer listener", "onReadyForSpeech");
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            Log.e("recognizer listener", "onPartialResults");
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            Log.e("recognizer listener", "onEvent");
        }

        @Override
        public void onError(int error) {
            Log.e("recognizer listener", "onError: " + String.valueOf(error));

            isSpeechRecognizerAlive = false;
        }

        @Override
        public void onEndOfSpeech() {
            Log.e("recognizer listener", "onEndOfSpeech");
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            Log.e("recognizer listener", "onBufferReceived");
        }

        @Override
        public void onBeginningOfSpeech() {
            Log.e("recognizer listener", "onBeginningOfSpeech");
            isSpeechRecognizerAlive = true;
        }

    };

    new CheckRecognizer().execute("");
}

private void checkIfRecognizerAslive(){
    Log.e("check", "check");
    if(!isSpeechRecognizerAlive){
        Log.e("check1", "check1");
        stopRecognition(new View(getApplicationContext()));
        startRecognition(new View(getApplicationContext()));
    }
}

@Override
protected void onResume() {
    super.onResume();
    Log.e("Start", "service");
}

@Override
protected void onPause() {
    super.onPause();
    Log.e("Stop", "service");
}


public void startRecognition(View view){
    Log.e("MainActivity", "startRecognition");
    isSpeechRecognizerAlive = false;
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
    mSpeechRecognizer.setRecognitionListener(mRecognitionListener);
    mSpeechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}

public void stopRecognition(View view){
    Log.e("MainActivity", "stopRecognition");
    if(mSpeechRecognizer != null){
        mSpeechRecognizer.stopListening();
        mSpeechRecognizer.cancel();
        mSpeechRecognizer.destroy();
        mSpeechRecognizer = null;
    }
}

private class CheckRecognizer extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {          
            @Override
            public void run() {
                publishProgress();
            }

        }, 0, 4000);


        return "";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {
        checkIfRecognizerAslive();
    }
}

}

I really don't know what I'm doing wrong. Can you help me find the issue please? Or does anybody know why my speech recognizer is busy even though I destroy it? Any suggestion is welcome. If you have other suggestions on how to do permanent voice recognition on Android it will be great. Thanks a lot!

Contraceptive answered 24/7, 2014 at 14:25 Comment(0)
G
0

When a recognizer cancel() is invoked after recognizer has started listening, it appears that this will always result in RecognitionListener.onError():

   18:43:01.842 native                    W  W0209 18:43:01.841218   26262 soda_async_impl.cc:861] SODA stopped processing audio, mics audio processed in millis: 2000, loopback audio processed in millis: 0
   18:43:01.845 A                         W  #onError space agsa_transcription_CANCELLED code -1!
   18:43:01.852                           W  Recognizer network error
                                             io.grpc.StatusException: CANCELLED
                                                at io.grpc.Status.asException(SourceFile:1)
                                                at com.google.android.apps.search.transcription.g.am.e(SourceFile:1)
                                                at com.google.android.apps.search.transcription.g.am.d(SourceFile:3)
                                                at com.google.android.apps.search.transcription.g.am.a(SourceFile:2)
                                                at com.google.android.apps.search.transcription.g.ac.a(SourceFile:1)
                                                at com.google.android.apps.search.transcription.g.av.a(SourceFile:2)
                                                at com.google.android.voicesearch.serviceapi.GoogleRecognitionService.onCancel(SourceFile:10)
                                                at android.speech.RecognitionService.dispatchCancel(RecognitionService.java:146)
                                                at android.speech.RecognitionService.-wrap1(Unknown Source:0)
                                                at android.speech.RecognitionService$1.handleMessage(RecognitionService.java:88)
                                                at android.os.Handler.dispatchMessage(Handler.java:106)
                                                at android.os.Looper.loop(Looper.java:176)
                                                at android.app.ActivityThread.main(ActivityThread.java:6662)
                                                at java.lang.reflect.Method.invoke(Native Method)
                                                at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
   18:43:01.855                           I  #markBufferClosed, read 64000 bytes
   18:43:01.856                           I  Audio focus releasing status: RELEASED
   18:43:01.856                           W  #read failed: read EOF from AudioAccessor.
   18:43:01.857                           W  #read failed: read EOF from AudioAccessor.
   18:43:01.857                           I  Sending end of audio to Soda.
   18:43:01.862                           I  startDetection successful
   18:43:01.863 native                    W  W0209 18:43:01.863233   26262 hybrid_selector_impl.cc:141] No active session.
Gash answered 9/2, 2023 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.