将语音发送到Android文本

时间:2014-02-11 07:18:38

标签: android speech-to-text android-speech-api

我一直在参考Android Speech Recognition as a service on Android 4.1 & 4.2帖子尝试在服务中实现语音识别。

我想我做得对。在我的设备上运行时,我收到了“准备好语音”的吐司消息,我在onReadyForSpeech()函数中声明了这个消息。

根据Hoan Nguyen为上述帖子提供ans的人,我们可以在调用onReadyForSpeech()函数后立即开始讲话。

我的问题是我不知道如何获得我们所说的演讲并将其转换为文本以及将其转换为何处。

有人知道怎么做吗?我知道这是一个非常蹩脚的问题,但它是我第一次使用语音识别。所以请耐心等待。

对此非常感谢。在此先感谢:)

    public class MyService 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;

    @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);
        //Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
        Log.d("onCreate","Entered");
    }


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

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

            Log.d("IncomingHandler","Entered");
        }


        @Override
        public void handleMessage(Message msg)
        {
            Log.d("handleMessage","Entered");

            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  
                        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
            Log.d("onTick","Entered");
        }

        @Override
        public void onFinish()
        {
            Log.d("onFinish","Entered");

            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) {
        // TODO Auto-generated method stub
        //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

        try
        {
            Message msg = new Message();
            msg.what = MSG_RECOGNIZER_START_LISTENING; 
            mServerMessenger.send(msg);
        }
        catch (RemoteException e)
        {
            Log.d("msg",""+e);
        }
        return  START_NOT_STICKY;
        //return super.onStartCommand(intent, flags, startId);
    }

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

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

        Log.d("onDestroy","Entered");
    }

    protected class SpeechRecognitionListener implements RecognitionListener
    {

        private static final String TAG = "Sppech---->";

        @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$
            Log.d("onBeginningOfSpeech","Entered");
        }

        @Override
        public void onBufferReceived(byte[] buffer)
        {
            String sTest = "";
            Log.d("onBufferReceived","Entered");
        }

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

        @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$
             Log.d("onError","Entered");
        }

        @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 >= Build.VERSION_CODES.JELLY_BEAN)
            {
                mIsCountDownOn = true;
                mNoSpeechCountDown.start();
                mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
            }
            //Log.d("TAG", "onReadyForSpeech"); 
            Toast.makeText(getApplicationContext(), "Ready for Speech", Toast.LENGTH_SHORT).show();
            Log.d("onReadyForSpeech","Entered");//$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 intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

你可以在onResult(Bundle result)中得到它,然后你可以得到用户对ArrayList的说法

ArrayList<String> matches = result.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);