0

我正在尝试创建一个应用程序,该应用程序仅检测用户可以对设备说出的特定短语,并且活动将根据用户所说的内容执行某些操作。我很难找到关于这个特定事物的教程,所以请帮助我。到目前为止,我已经创建了一个按钮来启动识别器意图,并且我有一个 onActivityResult,我希望它可以检测到用户在说什么,然后根据用户所说的短语调用特定的函数。

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

2 回答 2

1

这不是你应该如何使用它。它不像其他活动。您甚至还没有创建 SpeechRecognizer。请参阅SpeechRecognizer文档并查看createSpeechRecognizer()方法。您还将看到此活动的不同类型的结果。例如 onError() onResults() onEndOfSpeech() 等。你应该先看一个例子。很抱歉,我不能给你一个解决你问题的答案,但你的问题是你走错了路。

于 2015-03-07T20:32:47.027 回答
1

看看这个,你的代码应该是这样工作的:

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

将所需的字符串作为t1t2

希望这会有所帮助,如果有,请投票或接受。

于 2014-11-30T00:16:24.337 回答