如何:将语音命令转换为Android应用程序
有许多教程可以在线向Android应用程序添加语音识别。它们往往令人困惑,编码的出版商从来不会提出问题。我需要一个带有完整编码的简单教程,以将语音识别添加到我的应用中。如何:将语音命令转换为Android应用程序
如果你想添加语音识别到你的组的android应用程序,这是非常简单的。
在整个教程中,您需要在粘贴代码时添加导入。
- 创建一个XML文件,或使用一个现有的,并确保你添加一个按钮和一个ListView。
- 您需要在java类中扩展活动并实现OnClickListener 您将看到一个错误,指出您有未实现的方法。将鼠标悬停在其上并添加未实现的方法。我们将在稍后添加。
-
接下来在你的java中设置按钮和listview。
public ListView mList; public Button speakButton;
还补充:
public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
-
接下来,做出onCreate方法和设置按钮和监听。
speakButton = (Button) findViewById(R.id.btn_speak); speakButton.setOnClickListener(this);
也加入这个方法(我们将它设置旁边)
voiceinputbuttons();
记住的setContentView为你呈现的XML。
-
你的oncreate之外做一个新的方法,看起来像这样。现在
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); }
-
接下来,从第2步你的onclick方法内从第6步
startVoiceRecognitionActivity();
-
接下来我们将不得不建立另一种方法添加的活性。复制并粘贴以下代码。
@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));
匹配是语音输入的结果。这是用户可能说的内容的列表。对你想使用的关键字使用if语句允许使用任何活动,如果关键字匹配,可以设置多个关键字来使用相同的活动,这样多个单词将允许用户使用活动(使其成为可能用户不必记忆列表中的单词)要从语音输入信息中使用活动,只需使用以下格式;
if (matches.contains("information")) { informationmenu(); }
注意:您可以通过在eclipse中按ctrl + shift + F随时格式化代码。
-
现在我们将在步骤8中设置我们的代码使用的代码。此代码创建一个意图,将用户引导至新菜单。你将需要另外一个xml和java类。另外,请记住将活动添加到清单。
public void informationMenu() { startActivity(new Intent("android.intent.action.INFOSCREEN")); }
-
最后,您需要设置一些代码,让用户知道麦克风是否可操作。将此代码粘贴到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"); }
最后请注意:语音识别将无法在虚拟仿真工作,因为他们无法访问您的计算机上的麦克风。语音识别只能与互联网连接。
这是约。你的最终代码应该在你的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();
}
}
}
真的很好的教程。做得好。
要完成多一点点:
您需要的权限添加到您的清单如下
<uses-permission android:name="android.permission.RECORD_AUDIO" />
此外,如果有
launchMode="singleInstance"
或launchMode="singleTask"
它看起来像声音不工作它应该是“标准”
你是真棒....好教程 – Pragnani 2013-01-21 05:09:36
@Sam这对互联网连接很好。但是,有没有像他没有互联网连接?我想要它的简单命令,如打开消息框''打开浏览器''打开拨号键盘等。 – stack 2013-12-12 11:49:59
@stack使用Google Now集成? – milosmns 2014-08-31 21:16:37