Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpeechRecognizer : not connected to recognition service

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 ?

like image 676
Jeevan Avatar asked Nov 05 '12 06:11

Jeevan


People also ask

What is the speechrecognizer class?

This class provides access to the speech recognition service. This service allows access to the speech recognizer. Do not instantiate this class directly, instead, call createSpeechRecognizer (Context). This class's methods must be invoked only from the main application thread.

What is the purpose of the speech recognition service class?

This class provides access to the speech recognition service. This service allows access to the speech recognizer. Do not instantiate this class directly, instead, call createSpeechRecognizer (Context).

What is speechrecognitioncanceledeventargs?

Callbacks connected to this signal are called with a SpeechRecognitionCanceledEventArgs, instance as the single argument. Signal for events containing final recognition results (indicating a successful recognition attempt).

Is speech recognition asynchronous or asynchronous?

Starts speech recognition on a continuous audio stream as an asynchronous operation, until StopContinuousRecognitionAsync () is called. You must subscribe to events to receive recognition results.


2 Answers

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();
}
like image 134
Jeevan Avatar answered Sep 20 '22 14:09

Jeevan


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

like image 36
shady31 Avatar answered Sep 18 '22 14:09

shady31