Android Speech recognition

3k views Asked by At

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. I had a hard time finding tutorials on this specific thing so please help me out. So far I have created a button that will start the Recognizer Intent and I have a onActivityResult which I hope can detect what the user is saying and then call specific functions depending on the phrase the user has spoken.

public void OnClick_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);

}

protected void onActivityResult(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

        Toast.makeText(getApplicationContext(), "sound detected", Toast.LENGTH_LONG).show();
    }
}
2

There are 2 answers

3
Saikiran Sondarkar On BEST ANSWER

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

 public void OnClick_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);
 }

 protected void onActivityResult(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);
        } else if (((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.

0
Zgrkpnr__ On

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.