8

In my app, am using SpeechRecognizer directly. I destroy SpeechRecognizer onPause of the Activity and I recreate it in onResume method as below ...

public class NoUISpeechActivity extends Activity {

protected static final String CLASS_TAG = "NoUISpeechActivity";
private SpeechRecognizer sr;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_no_uispeech);

    sr = getSpeechRecognizer();
}

@Override
protected void onPause() {

    Log.i(CLASS_TAG, "on pause called");
    if(sr!=null){
        sr.stopListening();
        sr.cancel();
        sr.destroy();       

    }

    super.onPause();
}


@Override
protected void onResume() {

    Log.i(CLASS_TAG, "on resume called");       

    sr = getSpeechRecognizer();

    super.onResume();
}

....

private SpeechRecognizer getSpeechRecognizer() {
    if(sr == null){
        sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
        CustomRecognizerListner listner = new CustomRecognizerListner();
        listner.setOnListeningCallback(new OnListeningCallbackImp());
        sr.setRecognitionListener(listner);
    }
    return sr;
}
}

When the app is installed via eclipse for the first time, SpeechRecognition service is called and recognition happens properly.But when app comes back from pause, if i try to recognize speech i get "SpeechRecognition: not connect to recognition service" error

What am i doing wrong ?

Jeevan
  • 8,532
  • 14
  • 49
  • 67

2 Answers2

6

I found the reason to the problem. In onPause method though SpeechRecognition.destroy() method is called, I guess it just detaches the service but the object sr will be pointing to some instance and it wont be null. Resetting the object sr to null would solve the problem.

Not destroying the SpeechRecognition object in onPause method would block other apps from using SpeechRecognition service

@Override
protected void onPause() {

    Log.i(CLASS_TAG, "on pause called");
    if(sr!=null){
        sr.stopListening();
        sr.cancel();
        sr.destroy();              

    }
    sr = null;

    super.onPause();
}
Jeevan
  • 8,532
  • 14
  • 49
  • 67
  • 1
    yes. I think it is best to lazy load the SpeechRecognizer object. – gregm Nov 06 '12 at 10:40
  • Is there a way to resume SpeechRecognizer for the main activity and then destroy it when moving to a second activity within an app. At the moment I am getting conflicts between SpeechRecognizer and Googles Speech to Text in two seperate activities. Are you able to provide any suggestion @JeeZ – N MC May 19 '16 at 01:35
  • Sorry for the delayed reply, yes you can do that. First destroy the SpeechRecognizer in onStop() of the activity when you move out of the activity and Instantiate again in onRestart(). – Jeevan May 24 '16 at 10:52
3

Just stop calling stopListening() and cancel() methods. Instead call destroy() methods only. this should fix the problem :)

shady31
  • 101
  • 4