0

I'm working for the speech recognition for my application and I tried this code and I'm getting the error in my logcat as intent must not null exception in android

    public class SpeechRecognizerActivity extends Activity {
        /** Called when the activity is first created. */
        String a;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           final TextView txt=(TextView)findViewById(R.id.textView1);
            class MyRecognitionListener implements RecognitionListener {

                            @Override
                            public void onBeginningOfSpeech() {
                                    Log.d("Speech", "onBeginningOfSpeech");
                            }

                            @Override
                            public void onBufferReceived(byte[] buffer) {
                                    Log.d("Speech", "onBufferReceived");
                            }

                            @Override
                            public void onEndOfSpeech() {
                                    Log.d("Speech", "onEndOfSpeech");
                            }

                            @Override
                            public void onError(int error) {
                                    Log.d("Speech", "onError");
                            }

                            @Override
                            public void onEvent(int eventType, Bundle params) {
                                    Log.d("Speech", "onEvent");
                            }

                            @Override
                            public void onPartialResults(Bundle partialResults) {
                                    Log.d("Speech", "onPartialResults");
                            }

                            @Override
                            public void onReadyForSpeech(Bundle params) {
                                    Log.d("Speech", "onReadyForSpeech");
                            }

                            @Override
                            public void onResults(Bundle results) {
                                    Log.d("Speech", "onResults");
                                    ArrayList<String> strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                                    for (int i = 0; i < strlist.size();i++ ) {
                                            Log.d("Speech", "result=" + strlist.get(i));
                                    }

                            }

                            @Override
                            public void onRmsChanged(float rmsdB) {
                                    Log.d("Speech", "onRmsChanged");
                            }

            }
            SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
            MyRecognitionListener listener = new MyRecognitionListener();
            sr.setRecognitionListener(listener);
   sr.startListening(RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()));
         }
    }

Please suggest me, what i'm making mistake or have to set something setting previously in the emulator before running the program.

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
user12378334
  • 117
  • 1
  • 14
  • What are you doing in your code? How are you trying to recognize voice i just can't understand?!!! Have you preferred any tutorial for this before implmenting ? – GrIsHu Oct 14 '13 at 10:06
  • Check out this http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html – GrIsHu Oct 14 '13 at 10:09
  • @GrIsHu I think user2582340 doesn't want to open the speech input activity and thus tries to use the SpeechRecognizer, as asked ans answered here: http://stackoverflow.com/questions/6316937/how-can-i-use-speech-recognition-without-the-annoying-dialog-in-android-phones – Jan Z. Oct 14 '13 at 11:51
  • Any results so far? Did one of the answers work out for you? – Jan Z. Oct 14 '13 at 16:59

2 Answers2

1

I don't know if you're going in the right direction when trying to create the Intent for your SpeechRecognizer with RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()), as there are some restrictions using this method according to the documentation. These restrictions might lead to null returned by the function.

Try the following instead:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); // Replace by your package.
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
sr.startListening(intent);

as suggested by this SO thread.

Community
  • 1
  • 1
Jan Z.
  • 6,883
  • 4
  • 23
  • 27
1

Don't know whether you properly understood the VoiceRecognization or not. But Google provides an option to search the web using voice command .

Voice recognition featured in android is achieved using the RecognizerIntent.Use the Recognizer class in an intent to call the voice API.Here we check for the recognizer in the device if available the speech to text happen else we show a toast displaying 'Recognizer Not Found'

private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "AndroidBite Voice Recognition...");
startActivityForResult(intent, REQUEST_CODE);
}
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
     {
         ArrayList<String> matches = data.getStringArrayListExtra(
          RecognizerIntent.EXTRA_RESULTS);
           resultList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
      }
   super.onActivityResult(requestCode, resultCode, data);
}
}

Check out demo

GrIsHu
  • 29,068
  • 10
  • 64
  • 102