Skip to content Skip to sidebar Skip to footer

Android Speech Recognition

I am trying to create an app that simply detects specific phrases that the user can speak into the device and the activity will do something depending on what the user has spoken.

Solution 1:

Have a look at this, it's how your code should be to work:

publicvoidOnClick_Speed_Detector(View v) {
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "speak up");
    startActivityForResult(i, 1);
 }

 protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        //if user says the phrase "poptarts"//call function poptart//if user says the phrase "banana"//call function banana
        ArrayList < String > result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        ArrayList < String > result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


        if (((result).equals(t1))) {
            Intent intent = new Intent(this, * * ClassToOpen * * .class);
            startActivity(intent);
        } elseif (((result).equals(t2))) {
            Intent intent = new Intent(this, * * ClassToOpen * * .class);
            startActivity(intent);
        }

        Toast.makeText(getApplicationContext(), "sound detected", Toast.LENGTH_LONG).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
 }

Put the desired string as t1 and t2.

Hope this helps, vote up or accept if it did.

Solution 2:

This is not how you should use it. It is not like other activities. You haven't even created SpeechRecognizer. Please refer to SpeechRecognizer documentation and see createSpeechRecognizer() method. You will also see different kinds of result for this activity. For instance onError() onResults() onEndOfSpeech() etc. You should see an example first. I am sorry I couldn't give you an answer that solves your problem, but your problem is that you are on the wrong way.

Post a Comment for "Android Speech Recognition"