Android v< = ICS连续语音识别ERROR_NETWORK_TIMEOUT

时间:2014-02-25 15:08:59

标签: android speech-recognition

我通过以下this thread创建了一个连续语音识别Android应用程序,它在我的Moto G(4.4.2)上完美运行但由于某种原因它在2.3.5和2.3.6上崩溃,我已经尝试过删除4.1和更新版本的计时器解决方法,它是相同的。 问题在于,当我说出单词时它会正常工作,但是当它捕获一些噪音(比如鼠标点击)时,需要10秒识别(或者在onEndOfSpeech()之后执行的任何操作),然后它会抛出ERROR_NETWORK_TIMEOUT。

我的问题可能与this one有关,但在我的情况下,错误总是会发生。

我真的很感激这里的一些帮助。

更新

这是一个基于我提到的第一个链接的最小示例,它仍然不对我,另外我必须将服务添加到清单(<service android:name="com.test.MyService"></service>),activity_main.xml中的两个文本视图和RECORD_AUDIO权限。

MainActivity:

public class MainActivity extends Activity {

protected static final boolean DEBUG = false;
protected static final String TAG = null;
private int mBindFlag;
private MainActivity activityContext;
private Intent speechService;
public static TextView methodText;
public static TextView resultsText;
private static Messenger mServiceMessenger;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    methodText = (TextView) findViewById(R.id.textView1);
    resultsText = (TextView) findViewById(R.id.textView2);
    activityContext = this;
    speechService = new Intent(activityContext, MyService.class);
    activityContext.startService(speechService);
    mBindFlag = Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH ? 0 : Context.BIND_ABOVE_CLIENT;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onStart() {
    super.onStart();
    bindService(new Intent(this, MyService.class), mServiceConnection, mBindFlag);
}

@Override
protected void onStop()
{
    super.onStop();

    if (mServiceMessenger != null)
    {
        unbindService(mServiceConnection);
        mServiceMessenger = null;
    }
}

private final ServiceConnection mServiceConnection = new ServiceConnection()
{
    @Override
    public void onServiceConnected(ComponentName name, IBinder service)
    {
        if (DEBUG) {Log.d(TAG, "onServiceConnected");} //$NON-NLS-1$

        mServiceMessenger = new Messenger(service);
       sendMessage(MyService.MSG_RECOGNIZER_START_LISTENING);
    }

    @Override
    public void onServiceDisconnected(ComponentName name)
    {
        if (DEBUG) {Log.d(TAG, "onServiceDisconnected");} //$NON-NLS-1$
        mServiceMessenger = null;
    }

}; // mServiceConnection

public static void sendMessage(int type){

     Message msg = new Message();
     msg.what = type; 

     try
     {
         mServiceMessenger.send(msg);
     } 
     catch (RemoteException e)
     {
         e.printStackTrace();
     }
}

@Override
protected void onDestroy(){
    super.onDestroy();
    activityContext.stopService(speechService);
}
}

服务类:

public class MyService extends Service{
protected static AudioManager mAudioManager; 
protected SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));

protected boolean mIsListening;
protected volatile boolean mIsCountDownOn;
private static boolean mIsStreamSolo;

static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;
private static final String TAG = null;

@Override
public void onCreate()
{
    super.onCreate();

    MainActivity.methodText.setText("onCreate");

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                                     RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                                     this.getPackageName());
}

protected static class IncomingHandler extends Handler
{
    private WeakReference<MyService> mtarget;

    IncomingHandler(MyService target)
    {
        mtarget = new WeakReference<MyService>(target);
    }

    @Override
    public void handleMessage(Message msg)
    {
        MainActivity.methodText.setText("handleMessage");

        final MyService target = mtarget.get();

        switch (msg.what)
        {
            case MSG_RECOGNIZER_START_LISTENING:

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
                {
                    // turn off beep sound  
                    if (!mIsStreamSolo)
                    {
                        mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
                        mIsStreamSolo = true;
                    }
                }
                 if (!target.mIsListening)
                 {
                     target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
                     target.mIsListening = true;
                    //Log.d(TAG, "message start listening"); //$NON-NLS-1$
                 }
                 break;

             case MSG_RECOGNIZER_CANCEL:
                if (mIsStreamSolo)
               {
                    mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
                    mIsStreamSolo = false;
               }
                  target.mSpeechRecognizer.cancel();
                  target.mIsListening = false;
                  //Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$
                  break;
         }
   } 
} 

// Count down timer for Jelly Bean work around
protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{

    @Override
    public void onTick(long millisUntilFinished)
    {
        MainActivity.methodText.setText("onTick");
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish()
    {

        MainActivity.methodText.setText("onFinish");

        mIsCountDownOn = false;
        Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);
        try
        {
            mServerMessenger.send(message);
            message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
            mServerMessenger.send(message);
        }
        catch (RemoteException e)
        {

        }
    }
};

@Override
public void onDestroy()
{
    super.onDestroy();

    MainActivity.methodText.setText("onDestroy");

    if (mIsCountDownOn)
    {
        mNoSpeechCountDown.cancel();
    }
    if (mSpeechRecognizer != null)
    {
        mSpeechRecognizer.destroy();
    }
}

protected class SpeechRecognitionListener implements RecognitionListener
{
    private static final String TAG = "SpeechApp";

    @Override
    public void onBeginningOfSpeech()
    {
        MainActivity.methodText.setText("onBeginningOfSpeech");
        // speech input will be processed, so there is no need for count down anymore
        if (mIsCountDownOn)
        {
            mIsCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }               
        //Log.d(TAG, "onBeginingOfSpeech"); //$NON-NLS-1$
    }

    @Override
    public void onBufferReceived(byte[] buffer)
    {
        MainActivity.methodText.setText("onBufferReceived");
    }

    @Override
    public void onEndOfSpeech()
    {
        //Log.d(TAG, "onEndOfSpeech"); //$NON-NLS-1$
        MainActivity.methodText.setText("onEndOfSpeech");
     }

    @Override
    public void onError(int error)
    {
        MainActivity.methodText.setText("onError");

        if (mIsCountDownOn)
        {
            mIsCountDownOn = false;
            mNoSpeechCountDown.cancel();
        }
         mIsListening = false;
         Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
         try
         {
                mServerMessenger.send(message);
         }
         catch (RemoteException e)
         {

         }
        //Log.d(TAG, "error = " + error); //$NON-NLS-1$
    }

    @Override
    public void onEvent(int eventType, Bundle params)
    {
        MainActivity.methodText.setText("onEvent");
    }

    @Override
    public void onPartialResults(Bundle partialResults)
    {
        MainActivity.methodText.setText("onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle params)
    {
        MainActivity.methodText.setText("onReadyForSpeech");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            mIsCountDownOn = true;
            mNoSpeechCountDown.start();

        }
        Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
    }

    @Override
    public void onResults(Bundle results)
    {
        MainActivity.methodText.setText("onResults");

        System.out.println(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));
        MainActivity.resultsText.setText(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).toString());
        mIsListening = false;
        MainActivity.sendMessage(MyService.MSG_RECOGNIZER_START_LISTENING);
    }

    @Override
    public void onRmsChanged(float rmsdB)
    {
        MainActivity.methodText.setText("onRmsChanged");
    }

}

@Override
public IBinder onBind(Intent intent) {

    MainActivity.methodText.setText("onBind");

    // TODO Auto-generated method stub
    Log.d(TAG, "onBind");  //$NON-NLS-1$
    return mServerMessenger.getBinder();
}
}

0 个答案:

没有答案