繁体   English   中英

Android语音识别

[英]Android Speech recognition

我正在尝试创建一个应用程序,该应用程序仅检测用户可以在设备中说出的特定短语,而该活动将根据用户所说的内容执行某些操作。 我很难找到有关此特定主题的教程,因此请帮助我。 到目前为止,我已经创建了一个将启动Recognizer Intent的按钮,并且我具有一个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);
 }

将所需的字符串作为t1t2

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM