开发具备语音识别功能的 Android* 应用

laxila 2013-12-17 10:50:33
加精
分享一篇技术文章希望对大家有帮助
下载文章
开发具备语音识别功能的 Android* 应用 [PDF 398KB]

Android 不能识别语音,因此一款 Android 设备通常也不能识别语音。 通过什么方式帮助它识别呢?

最简便的方法就是让另一种应用为我们识别语音。 让另一种应用处理 Android 中的任务被称为 使用意图

我们的目标设备必须至少有一个可以为语音识别处理意图的应用,并且可以被 RecognizerIntent.ACTION_RECOGNIZE_SPEECH 调用。

其中的一款应用就是 Google 语音搜索。 该应用是当前 Android 可用的最好识别器之一,可以支持多种语言。 由于语音识别是通过 Google 服务器处理的,因此该服务要求必须有互联网连接。 该应用具备一个非常简单的活动,通过它告知用户是否可以讲话。 当用户停止讲话时,会话就会关闭,然后我们的应用(意图调用程序)将收到一列带有已识别语音的字符串。



语音识别示例
我们现在通过一个小的示例程序展示如何在应用中使用语音搜索。

我们的应用需要完成以下任务:

•收到语音识别请求
•检查语音识别应用的可用性
•如果语音识别可用,然后调用意图并接收结果
•如果语音识别不可用,然后会显示一个安装 Google 语音搜索的会话,并将用户重定向至 Google Play。
首先,我们需要创建一个类,为语音识别实现逻辑。 调用类 SpeechRecognitionHelper,此时我们需要声明一个静态的公共函数 run(),该函数将收到一个启动识别的请求:
/**
* A helper class for speech recognition
*/
public class SpeechRecognitionHelper {

/**
* Running the recognition process. Checks availability of recognition Activity,
* If Activity is absent, send user to Google Play to install Google Voice Search.
* If Activity is available, send Intent for running.
*
* @param callingActivity = Activity, that initializing recognition process
*/
public static void run(Activity callingActivity) {
// check if there is recognition Activity
if (isSpeechRecognitionActivityPresented(callingActivity) == true) {
// if yes – running recognition
startRecognition(callingActivity);
} else {
// if no, then showing notification to install Voice Search
Toast.makeText(callingActivity, "In order to activate speech recognition you must install \"Google Voice Search\"", Toast.LENGTH_LONG).show();
// start installing process
installGoogleVoiceSearch(callingActivity);
}
}
}
如您所见,除 run() 函数之外,我们还需要执行三个函数:•isSpeechRecognitionActivityPresented — 检查语音识别应用是否存在于系统中
•installGoogleVoiceSearch — 初始化 Google 语音搜索安装进程
•startRecognition — 准备适合的意图并运行识别
为了检查设备是否有语音识别应用,我们可以使用类 PackageManager 中的 queryIntentActivities 方法。 该方法列出了可以处理指定意图的各种活动。 为了接收 PackageManager 的一个实例,我们可以使用 getPackageManager。

我们的代码如下所示:

isSpeechRecognitionActivityPresented
/**
* Checks availability of speech recognizing Activity
*
* @param callerActivity – Activity that called the checking
* @return true – if Activity there available, false – if Activity is absent
*/
private static boolean isSpeechRecognitionActivityPresented(Activity callerActivity) {
try {
// getting an instance of package manager
PackageManager pm = callerActivity.getPackageManager();
// a list of activities, which can process speech recognition Intent
List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

if (activities.size() != 0) { // if list not empty
return true; // then we can recognize the speech
}
} catch (Exception e) {

}

return false; // we have no activities to recognize the speech
}
现在执行 startRecognition 函数。 该函数为启动语音识别活动提供适合的意图。 如欲了解有关该过程的详细信息,请查看 文档页。

源代码:
/**
* Send an Intent with request on speech
* @param callerActivity - Activity, that initiated a request
*/
private static void startRecognitionActivity(Activity callerActivity) {

// creating an Intent with “RecognizerIntent.ACTION_RECOGNIZE_SPEECH” action
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

// giving additional parameters:
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Select an application"); // user hint
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); // setting recognition model, optimized for short phrases – search queries
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); // quantity of results we want to receive
//choosing only 1st - the most relevant

// start Activity ant waiting the result
ownerActivity.startActivityForResult(intent, SystemData.VOICE_RECOGNITION_REQUEST_CODE);
}
最后,我们需要执行 installGoogleVoiceSearch。 该函数将会显示出一个会话,询问用户是否需要安装 Google 语音搜索,如果用户同意,则将其转至 Google Play。
/**
* Asking the permission for installing Google Voice Search.
* If permission granted – sent user to Google Play
* @param callerActivity – Activity, that initialized installing
*/
private static void installGoogleVoiceSearch(final Activity ownerActivity) {

// creating a dialog asking user if he want
// to install the Voice Search
Dialog dialog = new AlertDialog.Builder(ownerActivity)
.setMessage("For recognition it’s necessary to install \"Google Voice Search\"") // dialog message
.setTitle("Install Voice Search from Google Play?") // dialog header
.setPositiveButton("Install", new DialogInterface.OnClickListener() { // confirm button

// Install Button click handler
@Override
public void onClick(DialogInterface dialog, int which) {
try {
// creating an Intent for opening applications page in Google Play
// Voice Search package name: com.google.android.voicesearch
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.voicesearch"));
// setting flags to avoid going in application history (Activity call stack)
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// sending an Intent
ownerActivity.startActivity(intent);
} catch (Exception ex) {
// if something going wrong
// doing nothing
}
}})

.setNegativeButton("Cancel", null) // cancel button
.create();

dialog.show(); // showing dialog
}
大致就是这样。 我们运行语音识别活动。 然后请求用户的许可,安装语音搜索并在取得用户同意之后将其转至 Google Play。 我们还需要做的一件事就是收集语音识别的结果。

我们使用 startActivityForResult 函数发送一个请求,收集已启动活动的结果。 我们还需要在我们的意图调用程序活动中重新定义一个 OnActivityResult 方法。 按照以下方式进行定义:
// Activity Results handler
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

// if it’s speech recognition results
// and process finished ok
if (requestCode == SystemData.VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {

// receiving a result in string array
// there can be some strings because sometimes speech recognizing inaccurate
// more relevant results in the beginning of the list
ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

// in “matches” array we holding a results... let’s show the most relevant
if (matches.size() > 0) Toast.makeText(this, matches.get(0), Toast.LENGTH_LONG).show();
}

super.onActivityResult(requestCode, resultCode, data);
}
现在我们已经准备就绪
借助已创建的类 SpeechRecognitionHelper,我们只需调用一个函数 run() 就能执行一个语音识别请求。

此时只需在我们的项目中添加该类并在需要的位置中调用运行函数就能添加一个识别功能。 然后为发起识别请求的活动重新定义 onActivityResult 方法并执行处理文本结果。

如欲了解更多信息,请查看 Android 开发人员 网站。 这里的一些正确 示例 展示了如何实现语音识别,更重要的是,如何获取可用的语言列表。 如果您想要识别除用户默认位置之外的一种语言,您就需要使用该列表。

如果您想快速地将语音输入集成到您的应用中,您可以下载该代码并将其用于 SpeechRecognitionHelper 类。

原文转自:http://g.csdn.net/5260725
...全文
6352 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
yesthisisme 2015-10-23
  • 打赏
  • 举报
回复
tm-1680 2013-12-31
  • 打赏
  • 举报
回复
太深奥了,看不懂
shuizhuw 2013-12-27
  • 打赏
  • 举报
回复
太深奥了,看不懂
五叶绿茶 2013-12-26
  • 打赏
  • 举报
回复
老摩托 2013-12-25
  • 打赏
  • 举报
回复
太深奥了,看不懂
z0810 2013-12-25
  • 打赏
  • 举报
回复
woan521 2013-12-25
  • 打赏
  • 举报
回复
不懂的太多
zoumjej 2013-12-25
  • 打赏
  • 举报
回复
或许可以借鉴
treetor 2013-12-24
  • 打赏
  • 举报
回复
treetor 2013-12-24
  • 打赏
  • 举报
回复
好好学习新技术
q19890628 2013-12-23
  • 打赏
  • 举报
回复
这个东西不懂啊啊啊啊啊
lhw7791086 2013-12-22
  • 打赏
  • 举报
回复
s155 2013-12-22
  • 打赏
  • 举报
回复
看看还是不错的
心无止境007 2013-12-21
  • 打赏
  • 举报
回复
看看还是不错的
xusir98 2013-12-21
  • 打赏
  • 举报
回复
路边一青蛙 2013-12-20
  • 打赏
  • 举报
回复
我用过一个第三方的语音识别sdk....
alahe21 2013-12-19
  • 打赏
  • 举报
回复
看看还是不错的
nzmbzdn 2013-12-18
  • 打赏
  • 举报
回复
nzmbzdn 2013-12-18
  • 打赏
  • 举报
回复
好文,学习到了,谢谢楼主分享。
失败的谎言 2013-12-18
  • 打赏
  • 举报
回复
不需要这个 不过还是顶楼主
加载更多回复(1)

566

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧