SpeechRecognizer: not connected to the recognition service

In my application, I use SpeechRecognizer directly. I destroy SpeechRecognizer onPause from Activity, and I recreate it in the onResume method, as shown 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 application is installed through eclipse for the first time, the SpeechRecognition service is called and recognition is properly performed. But when the application returns from pause, if I try to recognize speech, I get the message "SpeechRecognition: not connect to detection service"

What am I doing wrong?

+4
source share
2 answers

I found the cause of the problem. In the onPause method, although the SpeechRecognition.destroy() method is called, I assume that it just separates the service, but the sr object will point to some instance and it will not be null. Resetting the sr object to zero may solve the problem.

Not destroying the SpeechRecognition object in the SpeechRecognition method blocks other applications from using the 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(); } 
+5
source

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

0
source

Source: https://habr.com/ru/post/1444046/


All Articles