简体   繁体   English

如何:将语音命令输入到 Android 应用程序中

[英]How To: Voice Commands into an android application

There are many tutorials online for adding voice recognition to an android app.网上有很多教程可以将语音识别添加到 Android 应用程序。 They are often confusing and the publishers of the coding are never available for questions.它们常常令人困惑,并且编码的发布者永远无法回答问题。 I need a basic overview of how to add voice recognition to my app (as an answer, not a link).我需要一个关于如何将语音识别添加到我的应用程序的基本概述(作为答案,而不是链接)。

If you want to add voice recognition to your group's Android app it is very simple.如果您想将语音识别添加到您小组的 Android 应用程序中,这非常简单。

Throughout this tutorial you will need to add imports as you paste in the code.在本教程中,您需要在粘贴代码时添加导入。

  1. Create an xml file or use an existing one and make sure that you add a button and a listview.创建一个 xml 文件或使用现有文件,并确保添加一个按钮和一个列表视图。
  2. In a java class you need to extend activity and implement OnClickListener You will get an error that says you have unimplemented methods.在 java 类中,您需要扩展活动并实现OnClickListener您将收到一条错误消息,提示您有未实现的方法。 Hover over it and add the unimplemented methods.将鼠标悬停在它上面并添加未实现的方法。 We will add to this later.我们稍后会对此进行补充。
  3. Next set up the button and listview in your java.接下来在您的 java 中设置按钮和列表视图。

     public ListView mList; public Button speakButton;

    also add:还添加:

     public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
  4. Next, make an OnCreate Method and set up the button and listener.接下来,制作一个 OnCreate 方法并设置按钮和侦听器。

     speakButton = (Button) findViewById(R.id.btn_speak); speakButton.setOnClickListener(this);

    also add this method(we will set it up next)也添加这个方法(我们接下来会设置它)

     voiceinputbuttons();

    Remember to setContentView for the xml you are showing.请记住为您显示的 xml 设置内容视图。

  5. Outside of your oncreate make a new method that looks like this.在你的 oncreate 之外创建一个看起来像这样的新方法。

     public void voiceinputbuttons() { speakButton = (Button) findViewById(R.id.btn_speak); mList = (ListView) findViewById(R.id.list); }
  6. Now you will have to set up your voice recognition activity by using the following code.现在您必须使用以下代码设置您的语音识别活动。

     public void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); }
  7. Next, inside your onclick method from step 2 add the activity from step 6.接下来,在第 2 步的 onclick 方法中添加第 6 步的活动。

     startVoiceRecognitionActivity();
  8. Next we will have to set up another method.接下来我们将不得不设置另一种方法。 Copy and paste the following code.复制并粘贴以下代码。

     @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));

    matches is the result of voice input. matches是语音输入的结果。 It is a list of what the user possibly said.它是用户可能说过的内容的列表。 Using an if statement for the keyword you want to use allows the use of any activity if keywords match it is possible to set up multiple keywords to use the same activity so more than one word will allow the user To use the activity (makes it so the user doesn't have to memorize words from a list) To use an activity from the voice input information simply use the following format;对要使用的关键字使用 if 语句允许使用任何活动,如果关键字匹配,可以设置多个关键字以使用相同的活动,这样多个词将允许用户使用该活动(使其如此用户不必记住列表中的单词)要使用语音输入信息中的活动,只需使用以下格式;

     if (matches.contains("information")) { informationMenu(); }

    NOTE: you can format the code any time by pressing ctrl+shift+F in eclipse.注意:您可以随时通过在 Eclipse 中按 ctrl+shift+F 来格式化代码。

  9. Now we are going to set up our method used by the code in step 8. This code creates an intent to direct the user to a new menu.现在我们将设置第 8 步中的代码使用的方法。此代码创建一个意图将用户定向到新菜单。 You will need another xml and java class for this.为此,您将需要另一个 xml 和 java 类。 Also, remember to add an activity to your manifest.另外,请记住向您的清单添加一个活动。

     public void informationMenu() { startActivity(new Intent("android.intent.action.INFOSCREEN")); }
  10. Finally you need to set up some code that will let the user know if the mic is operational.最后,您需要设置一些代码,让用户知道麦克风是否可用。 Paste this code inside of the OnCreate method at the end.最后将此代码粘贴到 OnCreate 方法中。

     // Check to see if a recognition activity is present // if running on AVD virtual device it will give this message. The mic // required only works on an actual android device PackageManager pm = getPackageManager(); List activities = pm.queryIntentActivities(new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size().= 0) { voiceButton;setOnClickListener(this). } else { voiceButton;setEnabled(false). voiceButton;setText("Recognizer not present"); }

FINAL NOTE: Voice Recognition will not work on a virtual emulator because they can't access the mic on your computer.最后注意:语音识别无法在虚拟模拟器上运行,因为它们无法访问您计算机上的麦克风。 The voice recognition will only work with an internet connection.语音识别仅适用于互联网连接。

This is approx.这是大约。 what your final code should look like in your java.你的最终代码在你的 java 中应该是什么样子。

package com.example.com.tutorialthread;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.speech.RecognizerIntent;
import android.support.v4.app.NavUtils;

public class main extends Activity implements OnClickListener {

public ListView mList;
public Button speakButton;

public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    speakButton = (Button) findViewById(R.id.btn_speak);
    speakButton.setOnClickListener(this);

    voiceinputbuttons();
}

public void informationMenu() {
    startActivity(new Intent("android.intent.action.INFOSCREEN"));
}

public void voiceinputbuttons() {
    speakButton = (Button) findViewById(R.id.btn_speak);
    mList = (ListView) findViewById(R.id.list);
}

public void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
        "Speech recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    startVoiceRecognitionActivity();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it
        // could have heard
        ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches));
        // matches is the result of voice input. It is a list of what the
        // user possibly said.
        // Using an if statement for the keyword you want to use allows the
        // use of any activity if keywords match
        // it is possible to set up multiple keywords to use the same
        // activity so more than one word will allow the user
        // to use the activity (makes it so the user doesn't have to
        // memorize words from a list)
        // to use an activity from the voice input information simply use
        // the following format;
        // if (matches.contains("keyword here") { startActivity(new
        // Intent("name.of.manifest.ACTIVITY")

        if (matches.contains("information")) {
            informationMenu();
        }
    }
}

Really good tutorial.真的很好的教程。 Well done.做得好。

To complete a little bit more:要完成更多一点:

You need to add permission to your manifest as follow您需要向您的清单添加权限,如下所示

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Also voice does not work if you have如果你有,语音也不起作用

launchMode="singleInstance" or launchMode="singleTask" it looks like it should be "standard" launchMode="singleInstance"launchMode="singleTask"看起来应该是“标准”

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

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