Android Speech recognition

给你一囗甜甜゛ 提交于 2020-03-25 17:25:09

问题


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

回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/27208081/android-speech-recognition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!