SpeechRecognizer:未连接到识别服务 [英] SpeechRecognizer : not connected to recognition service

查看:1181
本文介绍了SpeechRecognizer:未连接到识别服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我直接用SpeechRecognizer。我破坏活动的SpeechRecognizer的onPause我在onResume方法重新创建如下...

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;
}
}

当应用程序被通过Eclipse首次安装,语音识别服务被称为和认可情况properly.But当应用程序回来的暂停,如果我尝试识别语音,我得到​​语音识别:无法连接到识别服务的错误

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

我究竟做错了什么?

推荐答案

我找到了原因的问题。在的onPause 方法虽然 SpeechRecognition.destroy()方法被调用时,我想这只是脱离了服务,但对象 SR 将指向某些情况下,它不会是空的。重置对象 SR 为null会解决这个问题。

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.

不破坏语音识别对象的onPause 方法将阻止使用语音识别的其他应用程序服务

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();
}

这篇关于SpeechRecognizer:未连接到识别服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆