After evaluating several third-party speech recognition features, Google voice recognition is by far the most accurate. When using Google Voice Recognition, there are two main approaches. The easiest way is to run Intent and process the results accordingly:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE );
then in your onActivityResults () you will handle the matches returned by the service:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
The second approach is more involved, but allows you to better handle the error condition that may happen during the recognition service. Using this approach, you will create your own listening and callback methods. For example:
start listening:
mSpeechRecognizer.startListening(mRecognizerIntent);
where mRecognizerIntent:
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getBaseContext()); mSpeechRecognizer.setRecognitionListener(mRecognitionListener); mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); mRecognizerIntent.putExtra("calling_package", "com.you.package");
then create a listener:
private RecognitionListener mRecognitionListener = new RecognitionListener() { public void onBufferReceived(byte[] buffer) { // TODO Auto-generated method stub //Log.d(TAG, "onBufferReceived"); } public void onError(int error) { // TODO Auto-generated method stub // here is where you handle the error... public void onEvent(int eventType, Bundle params) { // TODO Auto-generated method stub Log.d(TAG, "onEvent"); } public void onPartialResults(Bundle partialResults) { // TODO Auto-generated method stub Log.d(TAG, "onPartialResults"); } public void onReadyForSpeech(Bundle params) { // TODO Auto-generated method stub Log.d(TAG, "onReadyForSpeech"); } public void onResults(Bundle results) { Log.d(TAG, ">>> onResults"); //Toast.makeText(getBaseContext(), "got voice results!", Toast.LENGTH_SHORT); ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); handleResults(matches); } public void onRmsChanged(float rmsdB) { // TODO Auto-generated method stub //Log.d(TAG, "onRmsChanged"); } public void onBeginningOfSpeech() { // TODO Auto-generated method stub Log.d(TAG, "onBeginningOfSpeech"); } public void onEndOfSpeech() { // TODO Auto-generated method stub Log.d(TAG, "onEndOfSpeech"); } };
you can add handleResults () to do whatever you want.