处理RecognitionListener错误 [英] Handling RecognitionListener Errors

查看:549
本文介绍了处理RecognitionListener错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的语音API,不断得到来自用户的输入。然而,这不工作的时候发生错误相当不错的。

我做的是重新启动在检测错误的方法监听器。它的工作原理一段时间,但识别器常挂一段时间。特别是经过检测服务器,网络超时,并识别忙错误。这是烦人!

我已经发现了一些试图解决这个问题,但他们没有为我工作。

你有一个更好的主意吗?

下面我我的code:

 私人无效startSR(){

    意图=新的意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //intent.putExtra(RecognizerIntent,值)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,mContext.getPackageName());

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,10);

    Log.d(TAG,语音识别开始了!);
    如果(识别器!= NULL){
        识别= NULL;
        mListener = NULL;
    }

    Log.d(TAG,setRecognitionListener);
    识别= SpeechRecognizer.createSpeechRecognizer(mContext);
    mListener =新监听器();

    recognizer.setRecognitionListener(mListener);

    recognizer.startListening(意向);

}
一流的监听器实现RecognitionListener {

    @覆盖
    公共无效onBeginningOfSpeech(){
        Log.i(TAG,onBeginningOfSpeech);
        MSTATUS =开始讲话;
    }

    @覆盖
    公共无效onBufferReceived(byte []的缓冲区){
        Log.i(TAG,onBufferReceived);

    }

    @覆盖
    公共无效onEndOfSpeech(){
        Log.i(TAG,onEndOfSpeech);
        MSTATUS =演讲结束;
    }


    @覆盖
    公共无效的onEvent(INT事件类型,捆绑PARAMS){
        Log.i(TAG的onEvent+ EVENTTYPE);

    }

    @覆盖
    公共无效onPartialResults(包partialResults){
        Log.i(TAG,onPartialResults);
        MSTATUS =部分结果;
    }

    @覆盖
    公共无效onReadyForSpeech(包PARAMS){
        Log.i(TAG,onReadyForSpeech);
        mReady = TRUE;
        MSTATUS =语音引擎准备好了;
    }
    @覆盖
    公共无效onRmsChanged(浮动rmsdB){
        // TODO自动生成方法存根

    }
    @覆盖
    公共无效onerror的(INT错误){
        // TODO自动生成方法存根
        mError =;
        MSTATUS =错误检测;
        开关(错误){
        案例SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            mError =网络超时;
            startListening();
            打破;
        案例SpeechRecognizer.ERROR_NETWORK:
            mError =网络;
            //吐司(请检查数据包或网络设置);
            返回;
        案例SpeechRecognizer.ERROR_AUDIO:
            mError =音频;
            打破;
        案例SpeechRecognizer.ERROR_SERVER:
            mError =服务器;
            startListening();
            打破;
        案例SpeechRecognizer.ERROR_CLIENT:
            mError =客户;
            打破;
        案例SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            mError =演讲超时;
            打破;
        案例SpeechRecognizer.ERROR_NO_MATCH:
            mError =不匹配;
            startListening();

            打破;
        案例SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            mError =识别器忙;
            打破;
        案例SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            mError =权限不足;
            打破;

        }
        Log.i(TAG,错误:+误差+ - + mError);

        // startSR();
    }


    @覆盖
    公共无效onResults(捆绑的结果){
        MSTATUS =得到了一些成果;
        mResultAvailable = TRUE;
        字符串str =新的String();
        Log.d(TAG,onResults+结果);

        mResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        // mConfidences = results.getDoubleArray(SpeechRecognizer.CONFIDENCE_SCORES);

        Log.i(TAG,的toString());

        startListening();


    }



} //类的监听器

公众的ArrayList<字符串> getResults(){
    返回mResults;
}
公共无效startListening(){
    如果(SpeechRecognizer.isRecognitionAvailable(mContext)){
        如果(识别器!= NULL){
            recognizer.startListening(意向);
            mResultAvailable = FALSE;
            mResults =新的ArrayList<字符串>();
        }
        其他
            startSR();
    }
}
 

解决方案
  • 如果你得到识别忙中出错,你得叫取消,然后调用 startListening
  • 如果你的服务器或网络错误,你必须在调用之前,检查网络连接 startListening

I am using the Android's speech API to continuously getting input from the user. However this doesn't work quite well when errors occur.

What I do is restarting the listener in the method that detects error. It works sometime but the recognizer hangs often for some time. Especially after detecting Server, network time out and recognizer busy errors. This is annoying!

I have found some attempt to solve this problem, but none of them worked for me.

Do you have a better idea?

Here i my code:

private void startSR(){

    intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //intent.putExtra(RecognizerIntent., value)
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, mContext.getPackageName());

    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);

    Log.d(TAG,"Speech recognition started!");
    if (recognizer != null) {
        recognizer = null;
        mListener = null;
    }

    Log.d(TAG,"setRecognitionListener");
    recognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
    mListener = new Listener();

    recognizer.setRecognitionListener(mListener);

    recognizer.startListening(intent);

}
class Listener implements RecognitionListener{

    @Override
    public void onBeginningOfSpeech() {
        Log.i(TAG, "onBeginningOfSpeech");
        mStatus = "Beginning speech";
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.i(TAG, "onBufferReceived");

    }

    @Override
    public void onEndOfSpeech() {
        Log.i(TAG, "onEndOfSpeech");
        mStatus = "Speech ended";
    }


    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.i(TAG, "onEvent " + eventType);

    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.i(TAG, "onPartialResults");
        mStatus = "Partial results";
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.i(TAG, "onReadyForSpeech");
        mReady = true;
        mStatus = "Speech engine ready";
    }
    @Override
    public void onRmsChanged(float rmsdB) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onError(int error) {
        // TODO Auto-generated method stub
        mError = "";
        mStatus = "Error detected";
        switch (error) {
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:                
            mError = " network timeout"; 
            startListening();
            break;
        case SpeechRecognizer.ERROR_NETWORK: 
            mError = " network" ;
            //toast("Please check data bundle or network settings");
            return;
        case SpeechRecognizer.ERROR_AUDIO: 
            mError = " audio"; 
            break;
        case SpeechRecognizer.ERROR_SERVER: 
            mError = " server"; 
            startListening();
            break;
        case SpeechRecognizer.ERROR_CLIENT: 
            mError = " client"; 
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
            mError = " speech time out" ; 
            break;
        case SpeechRecognizer.ERROR_NO_MATCH: 
            mError = " no match" ; 
            startListening();

            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
            mError = " recogniser busy" ; 
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
            mError = " insufficient permissions" ; 
            break;

        }
        Log.i(TAG,  "Error: " +  error + " - " + mError);

        //startSR();
    }


    @Override
    public void onResults(Bundle results) {
        mStatus = "Got some results";
        mResultAvailable = true;
        String str = new String();
        Log.d(TAG, "onResults " + results);

        mResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        //mConfidences = results.getDoubleArray(SpeechRecognizer.CONFIDENCE_SCORES);

        Log.i(TAG, toString());

        startListening();


    }



}// class Listener

public ArrayList<String> getResults(){
    return mResults;
}
public void startListening(){
    if (SpeechRecognizer.isRecognitionAvailable(mContext)) {
        if (recognizer!=null){
            recognizer.startListening(intent);
            mResultAvailable = false;
            mResults = new ArrayList<String>();
        }
        else
            startSR();
    }
}

解决方案

  • If you get recognizer busy error you have to call cancel and then call startListening
  • If you get server or network errors you have to check for network connection before calling startListening

这篇关于处理RecognitionListener错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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