Android speech Recognition App Without Pop Up

前端 未结 4 1336
天涯浪人
天涯浪人 2020-12-02 19:30

I\'m currently looking into getting a career with JAVA and have decided to start by building an app. I have this code right here that I am using to trigger Speech Recognitio

相关标签:
4条回答
  • 2020-12-02 19:44

    It's been a long time since the post. Still for those who are looking, the above code provided by Hoan is almost complete, but there is an important line missing. Both in question and answer and I am not sure how it could work without that.

    You need to create the SpeechRecognitionListener and set it as a listener for the SpeechRecognizer. Also it has to be done before we make a call to startListening() method of the SpeechRecognizer.

    SpeechRecognitionListener listener = new SpeechRecognitionListener(); mSpeechRecognizer.setRecognitionListener(listener);

    Then you also need to remove the listener from the onError event.

    0 讨论(0)
  • 2020-12-02 19:48

    AndroidManifest.xml

    Add the following permission:

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    

    class members

    private SpeechRecognizer mSpeechRecognizer;
    private Intent mSpeechRecognizerIntent; 
    private boolean mIslistening; 
    

    In onCreate

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        .........
        .........
        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        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());
    
    
        SpeechRecognitionListener listener = new SpeechRecognitionListener();
        mSpeechRecognizer.setRecognitionListener(listener);
    
    }   
    

    in your button listener just use this code

    if (!mIsListening)
    {
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    }
    

    In onDestroy

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

    Inside your activity create the inner class

    protected class SpeechRecognitionListener implements RecognitionListener
    {
    
        @Override
        public void onBeginningOfSpeech()
        {               
            //Log.d(TAG, "onBeginingOfSpeech"); 
        }
    
        @Override
        public void onBufferReceived(byte[] buffer)
        {
    
        }
    
        @Override
        public void onEndOfSpeech()
        {
            //Log.d(TAG, "onEndOfSpeech");
         }
    
        @Override
        public void onError(int error)
        {
             mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
    
            //Log.d(TAG, "error = " + error);
        }
    
        @Override
        public void onEvent(int eventType, Bundle params)
        {
    
        }
    
        @Override
        public void onPartialResults(Bundle partialResults)
        {
    
        }
    
        @Override
        public void onReadyForSpeech(Bundle params)
        {
            Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
        }
    
        @Override
        public void onResults(Bundle results)
        {
            //Log.d(TAG, "onResults"); //$NON-NLS-1$
            ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            // matches are the return values of speech recognition engine
            // Use these values for whatever you wish to do
        }
    
        @Override
        public void onRmsChanged(float rmsdB)
        {
        }
    }
    

    EDIT 2015-02-07: Incorporated code from the answers to this question by ZakiMak and Born To Win into the code in this answer to make this one more complete.

    0 讨论(0)
  • 2020-12-02 19:52

    I ran into that issue as well. It seems like having startActivityForResult(...) will enable the pop-up mic, then you can handle the response in onActivityResult(). However, simply adding that startActivityForResult messed up my startListening(mSpeechRecognizerIntent), so you may need to do more adjustment.

    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                                         RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                                         this.getPackageName());
    startActivityForResult(recognizerIntent, 100);
    
    // call back
    onActivityResult(int requestCode, int resultCode, Intent data){...}
    
    0 讨论(0)
  • 2020-12-02 19:58

    Don't Forget to add permission of following:-

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    
    0 讨论(0)
提交回复
热议问题