2

我在这里使用解决方案:Android Speech Recognition as a service on Android 4.1 & 4.2下面的代码到达 onStartCommand() 方法,但是语音识别似乎永远不会启动,事实证明 onReadyForSpeech()方法永远不会被调用。

更新:所以我添加并允许调用 onReadyForSpeech() ,但 onError() 调用错误代码: 6 在 onReadyForSpeech() 方法完成后(这进入连续循环,因为开始监听代码再次启动在调用 onError() 之后)。就像下面的 Hoan Nguyen 所说,错误代码 6 是 ERROR_SPEECH_TIMEOUT 但我从来没有看到出现语音识别对话框,所以我知道要开始说话了。我还尝试在启动此代码后立即开始讲话而没有出现该对话框,但仍然收到错误代码 6,我在这里做错了什么?

更新 (9-06-13):就像 Hoan 所说,没有出现对话。只要听到被调用,你就开始说话。但是我遇到了另一个问题,即在每次调用 onResult() 后尝试重新启动侦听器......该问题和完整代码可以在这里找到:Android Speech Speech Recognition: Repeated Calling of SpeechRecognizer.startListening() failed on JB 4.1.2

我调用服务的活动代码:

startService(new Intent(VoiceRecognition.this, VoiceRecogService.class));

我的服务:

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

        protected boolean mIsListening;
        protected volatile boolean mIsCountDownOn;

        static final int MSG_RECOGNIZER_START_LISTENING = 1;
        static final int MSG_RECOGNIZER_CANCEL = 2;

        private int mBindFlag;
        private Messenger mServiceMessenger;

        @Override
        public void onCreate()
        {
            super.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());

            //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
        }

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

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


            @Override
            public void handleMessage(Message msg)
            {
                final VoiceRecogService target = mtarget.get();

                switch (msg.what)
                {
                    case MSG_RECOGNIZER_START_LISTENING:

                        if (Build.VERSION.SDK_INT >= 16);//Build.VERSION_CODES.JELLY_BEAN)
                        {
                            // turn off beep sound  
                            target.mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, 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:
                          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)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void 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 int onStartCommand (Intent intent, int flags, int startId) 
        {
            //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
            try
            {
                Message msg = new Message();
                msg.what = MSG_RECOGNIZER_START_LISTENING; 
                mServerMessenger.send(msg);
            }
            catch (RemoteException e)
            {

            }
            return  START_NOT_STICKY;
        }

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

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

        protected class SpeechRecognitionListener implements RecognitionListener
        {

            @Override
            public void 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)
            {
                String sTest = "";
            }

            @Override
            public void onEndOfSpeech()
            {
                Log.d("TESTING: SPEECH SERVICE", "onEndOfSpeech"); //$NON-NLS-1$
             }

            @Override
            public void onError(int error)
            {
                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)
            {

            }

            @Override
            public void onPartialResults(Bundle partialResults)
            {

            }

            @Override
            public void onReadyForSpeech(Bundle params)
            {
                if (Build.VERSION.SDK_INT >= 16);//Build.VERSION_CODES.JELLY_BEAN)
                {
                    mIsCountDownOn = true;
                    mNoSpeechCountDown.start();
                    mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
                }
                Log.d("TESTING: SPEECH SERVICE", "onReadyForSpeech"); //$NON-NLS-1$
            }

            @Override
            public void onResults(Bundle results)
            {
                //Log.d(TAG, "onResults"); //$NON-NLS-1$

            }

            @Override
            public void onRmsChanged(float rmsdB)
            {

            }



        }

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    }
4

0 回答 0