- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
网上有很多教程可以将语音识别添加到安卓应用中。它们经常令人困惑,并且编码的发布者永远无法回答问题。我需要一个关于如何向我的应用添加语音识别的基本概述(作为答案,而不是链接)。
最佳答案
如果您想将语音识别添加到您小组的 Android 应用程序,这非常简单。
在本教程中,您需要在粘贴代码时添加导入。
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();
记得为你显示的 xml 设置内容 View 。
在你的 oncreate 之外创建一个看起来像这样的新方法。
public void voiceinputbuttons() {
speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
}
现在您必须使用以下代码设置语音识别 Activity 。
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 步中的 Activity 。
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));
matches 是语音输入的结果。它是用户可能说的话的列表。对要使用的关键字使用 if 语句允许使用任何 Activity ,如果关键字匹配,可以设置多个关键字以使用相同的 Activity ,因此多个单词将允许用户使用 Activity (使其如此用户不必记住列表中的单词)要使用语音输入信息中的 Activity ,只需使用以下格式;
if (matches.contains("information")) {
informationMenu();
}
注意:你可以在eclipse中按ctrl+shift+F随时格式化代码。
现在我们将设置第 8 步中的代码使用的方法。此代码创建了一个将用户引导至新菜单的 Intent 。为此,您将需要另一个 xml 和 java 类。另外,请记住在 list 中添加一个 Activity 。
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();
}
}
}
关于java - 如何: Voice Commands into an android application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798337/
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以使溢出。 3年前关闭。 o
首先,C++ 不是我的语言(我是 java 程序员)。 我看过 Microsoft SAPI 5.1 的示例,我需要一种方法来打印机器中所有可用的语音。我看过 Object token SAPI,我有
这是一个听起来很愚蠢的问题,但我试图在常见问题解答和论坛中寻找答案,但没有找到答案。 就像您可以在移动浏览器的超链接中使用 tel: 一样,您可以设置一个链接以使用 Skype 的软件开始通话: Ca
我在我的应用程序中使用Agora Voice Unity SDK进行语音呼叫。我面临着一个问题,当我离开iPhone的频道时。我的按钮卡丁声停止工作。。通过调查,我发现“AVAudioSession”
我刚刚构建了一个在谷歌助手上运行的语音对话应用程序。但是,我无法弄清楚如何录制此应用程序的演示以与其他人分享。我正在使用 iPhone 和 Mac。视频录制部分工作正常,但我无法录制语音对话。 我尝试
我完全不知所措。当我打电话时: var a = VoiceCommandDefinitionManager.InstalledCommandDefinitions; 或 await Windows.A
我最近使用说话人识别库 - ALIZE 进行编程。但是我有一些关于如何安装和执行演示的问题。有人可以帮我看看我的安装和执行顺序有什么问题。 我使用 Mac OS 系统: 1、下载lib ALIZE和L
我正在尝试使用Pocketsphinx(。进行简单的语音到文本映射。语法非常简单,例如: public = (Matt, Anna, Tom, Christine)+ (One | Two | Th
我正在尝试创建一个 Google 操作,但出现此错误: Your sample invocations are structured incorrectly. Make sure they all i
我需要做些类似的事情:例如获取我的声音片段,然后从中提取声音打印,然后使用它生成具有“MY”音调的声音 暗示,想法或做类似事情的事情会很有帮助 最佳答案 尝试使用Audacity 关于voice-re
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 1 年前。 Improve this ques
我在 Google API for speech recognition 上取得了优异的成绩自然对话,但是对于 Youtube 视频或电影中的声音识别很差或不存在。 在 iPhone 4 上用西类牙语
我已经创建了一个语音控制的 android 应用程序。我提供了将语言环境更改为日语的选项,英语是默认语言。 日语 TTS 非常好用。但是当语音识别出现时,日语单词被识别为英语单词,因此与英语单词进行匹
语音转子选项之一是打字模式。 当打字模式为标准时,您触摸一个键即可听到该键的作用,您必须双击它才能真正使用它。 当输入模式为盲打时,键盘的行为几乎就像没有打开旁白一样。 有没有办法知道“键入模式”何时
我在 Galaxy S4 中使用 Android 5.0。当我双击主页按钮时,S 语音打开。我想让代码打开它。我使用了下面的代码,但代码只打开谷歌语音。如何在没有任何配置的情况下打开 S Voice?
是否有一种方法(最好向后兼容 Mac OS X 10.3)来判断“系统偏好设置”中的“Voice Over”是否已激活? 最佳答案 这似乎存储在通用访问的首选项文件中。应用程序标识符是“com.app
当我在iOS Voice Over模式下单击时,它将读取标记的元素,但是我想知道标记点x和y,是否有任何API可以获取? 最佳答案 您无法从VoiceOver获得此信息。 API不支持它。您能获得的最
有没有办法阻止 VoiceOver/TalkBack 使用 javascript/CSS 从文本输入中读取 readOnly 属性? 我不希望在 VO 上读取此只读属性。 最佳答案 如果您不希望声明
[可能重复]但我没有在下面找到问题的答案。 是否可以将语音识别作为服务运行?我想实现这样的事情:虽然我的电话通过语音识别处于 sleep 模式,但我需要调用一个号码。除了语音识别,还有什么传感器可以检
我是一名优秀的程序员,十分优秀!