Android speech Recognition App Without Pop Up Android speech Recognition App Without Pop Up android android

Android speech Recognition App Without Pop Up


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

@Overrideprotected 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.


Don't Forget to add permission of following:-

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


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.