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!
 
     
    