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();
}
}
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.