如何:语音命令到Android应用程序

Sam*_*ins 55 java android voice-recognition

有很多在线教程可以为Android应用添加语音识别功能.它们经常令人困惑,编码的出版商永远无法提问.我需要一个简单的教程,完整的编码,为我的应用添加语音识别.

Sam*_*ins 93

如果你想为你的群组的Android应用程序添加语音识别,这非常简单.

在本教程中,您将需要在粘贴代码时添加导入.

  1. 创建一个xml文件或使用现有文件,并确保添加一个按钮和一个列表视图.
  2. 在java类中,您需要扩展活动并实现OnClickListener您将收到一条错误消息,指出您有未实现的方法.将鼠标悬停在它上面并添加未实现的方法.我们稍后会添加.
  3. 接下来在java中设置button和listview.

    public ListView mList;
    public Button speakButton;
    
    Run Code Online (Sandbox Code Playgroud)

    还添加:

    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    
    Run Code Online (Sandbox Code Playgroud)
  4. 接下来,创建一个OnCreate方法并设置按钮和监听器.

    speakButton = (Button) findViewById(R.id.btn_speak);
    speakButton.setOnClickListener(this);
    
    Run Code Online (Sandbox Code Playgroud)

    还添加此方法(我们将在下一步设置)

    voiceinputbuttons();
    
    Run Code Online (Sandbox Code Playgroud)

    请记住为您要显示的xml设置SetContentView.

  5. 在你的oncreate之外创建一个看起来像这样的新方法.

    public void voiceinputbuttons() {
        speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.list);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. 现在,您必须使用以下代码设置语音识别活动.

    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);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  7. 接下来,在步骤2中的onclick方法内添加步骤6中的活动.

    startVoiceRecognitionActivity();
    
    Run Code Online (Sandbox Code Playgroud)
  8. 接下来,我们将不得不设置另一种方法.复制并粘贴以下代码.

    @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));
    
    Run Code Online (Sandbox Code Playgroud)

    匹配是语音输入的结果.它是用户可能说的内容列表.对要使用的关键字使用if语句允许使用任何活动,如果关键字匹配,则可以设置多个关键字以使用相同的活动,因此多个单词将允许用户使用该活动(使其成为可能)用户不必记住列表中的单词.要使用语音输入信息中的活动,只需使用以下格式;

    if (matches.contains("information")) {
        informationmenu();
    }
    
    Run Code Online (Sandbox Code Playgroud)

    注意:您可以通过在eclipse中按ctrl + shift + F随时格式化代码.

  9. 现在我们将设置我们在步骤8中使用的方法.该代码创建了一个将用户引导到新菜单的意图.你需要另一个xml和java类.另外,请记住向清单添加活动.

    public void informationMenu() {
        startActivity(new Intent("android.intent.action.INFOSCREEN"));
    }
    
    Run Code Online (Sandbox Code Playgroud)
  10. 最后,您需要设置一些代码,让用户知道麦克风是否正常运行.最后将此代码粘贴到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");
    }
    
    Run Code Online (Sandbox Code Playgroud)

最后注意:语音识别无法在虚拟仿真器上运行,因为它们无法访问计算机上的麦克风.语音识别仅适用于互联网连接.

这是约.你的最终代码应该在你的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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Sam这对互联网连接有好处.但是,有什么像他没有互联网连接?我希望它用于简单的命令,例如:打开消息框''打开浏览器''打开拨号盘'等. (5认同)
  • 如何从服务中不断听? (3认同)

小智 9

真的很好的教程.做得好.

完成一点:

您需要为清单添加权限,如下所示

<uses-permission android:name="android.permission.RECORD_AUDIO" />
Run Code Online (Sandbox Code Playgroud)

如果你有话,声音也不起作用

launchMode="singleInstance"或者launchMode="singleTask"它看起来应该是"标准"