Android语音识别

Android语音识别,android,voice-recognition,recognizer-intent,Android,Voice Recognition,Recognizer Intent,我正在尝试创建一个应用程序,它只检测用户可以对设备说的特定短语,活动将根据用户所说的内容进行操作。我很难找到这方面的教程,所以请帮助我。到目前为止,我已经创建了一个按钮,可以启动识别器意图,我有一个onActivityResult,我希望它可以检测用户在说什么,然后根据用户所说的短语调用特定的函数 public void OnClick_Speed_Detector(View v) { Intent i = new Intent(RecognizerIntent.ACTION_RECOG

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

请看一看,您的代码应该如何工作:

 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);
 }
public void OnClick\u Speed\u检测器(视图v){
意图i=新意图(识别者意图、行动、识别、讲话);
i、 putExtra(RecognizerIntent.EXTRA_语言,
识别者意图、语言、模型、自由形式);
i、 putExtra(RecognizerIntent.EXTRA_提示符,“大声说”);
startActivityForResult(i,1);
}
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==1&&resultCode==RESULT\u确定){
//如果用户说短语“PopStarts”
//调用函数postart
//如果用户说短语“香蕉”
//调用函数香蕉
ArrayListresult=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ArrayListresult=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
如果(((result).equals(t1))){
意向意向=新意向(此,**ClassToOpen**.class);
星触觉(意向);
}else如果((result).equals(t2))){
意向意向=新意向(此,**ClassToOpen**.class);
星触觉(意向);
}
Toast.makeText(getApplicationContext(),“检测到声音”,Toast.LENGTH_LONG.show();
}
super.onActivityResult(请求代码、结果代码、数据);
}
将所需字符串设置为t1t2


希望这有帮助,投票支持或接受它。

这不是你应该使用它的方式。这与其他活动不同。你甚至还没有创建语音识别器。请参阅文档并参阅CreateSpeechRecognitor()方法。您还将看到此活动的不同结果。例如onError()onResults()onEndOfSpeech()等。您应该首先看到一个示例。很抱歉,我不能给你一个解决问题的答案,但你的问题是你走错了路。

好的,那么问题是什么?问题是当某些短语被说出时,我如何检测到……比如如果用户说PopStarts,我如何编写代码,以便应用程序能够对短语“PopStarts”做出反应而不是别的词?那么你介意用实际问题更新这个问题吗?目前还不太清楚你在问什么。你可以在这里找到运行识别字符串分析的代码,例如:你好,我试图通过用祝酒来改变意图来测试这一点,而不是显示一个通知,如果我说的单词确实被识别,但它工作不正常。这是一张写了什么的图片哦,你能把***.java的全部代码都贴出来吗,这样我就能发现错误了……:)@SSJ3GOKU878我真的没有太多其他的代码,这里是我对你的代码所做的,但我没有得到任何结果。我按下按钮激活语音识别器,说出输入代码的单词,但什么也没发生。