我正在尝试创建一个应用程序,该应用程序仅检测用户可以对设备说出的特定短语,并且活动将根据用户所说的内容执行某些操作。我很难找到关于这个特定事物的教程,所以请帮助我。到目前为止,我已经创建了一个按钮来启动识别器意图,并且我有一个 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();
}
}