Android 轻松实现语音识别详解及实例代码

使用Intent调用语音识别程序

说明

Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到语音识别设备,就会抛出异常 ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google 云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别!

注意:使用前需要安装语音识别程序。如《语音搜索》,其使用的语音识别技术来自于Google,Intent可以识别到该程序。

本例参考自android例程:

development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java

核心代码及说明

package com.example.test; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements OnClickListener { private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); // 识别按钮 PackageManager pm = getPackageManager(); List activities = pm.queryIntentActivities(new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); // 本地识别程序 // new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // 网络识别程序 /* * 此处没有使用捕捉异常,而是检测是否有语音识别程序。 * 也可以在startRecognizerActivity()方法中捕捉ActivityNotFoundException异常 */ if (activities.size() != 0) { btn.setOnClickListener(this); } else { // 若检测不到语音识别程序在本机安装,测将扭铵置灰 btn.setEnabled(false); btn.setText("未检测到语音识别设备"); } } public void onClick(View v) { if (v.getId() == R.id.btn) { startRecognizerActivity(); } } // 开始识别 private void startRecognizerActivity() { // 通过Intent传递语音识别的模式,开启语音 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // 语言模式和自由模式的语音识别 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // 提示语音开始 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音"); // 开始语音识别 startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); // 调出识别界面 } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // 回调获取从谷歌得到的数据 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { // 取得语音的字符 ArrayList<String> results = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String resultString = ""; for (int i = 0; i < results.size(); i++) { resultString += results.get(i); } Toast.makeText(this, resultString, Toast.LENGTH_SHORT).show(); } // 语音识别后的回调,将识别的字串以Toast显示 super.onActivityResult(requestCode, resultCode, data); } }

其主要原理就是将语音发送到google云端,然后云端处理,匹配相应的数据,发送到客户端。

最后不要忘记,在manifest中加入网络访问权限:

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

运行后效果:

以上就是对Android 实现语音识别的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

时间: 2024-08-20 00:09:12

Android 轻松实现语音识别详解及实例代码的相关文章

Android 轻松实现语音识别详解及实例代码_Android

使用Intent调用语音识别程序 说明 Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到语音识别设备,就会抛出异常 ActivityNotFoundException,所以我们需要捕捉这个异常.而且语音识别在模拟器上是无法测试的,因为语音识别是访问google 云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别! 注意:使用前需要安装语音识别程序.如<语音搜索>

Android UI 实现老虎机详解及实例代码

Android UI 实现老虎机详解 listview 的使用步骤 简单的listview老虎机实现 1.实现效果图 2.需要掌握的知识 listview的使用步骤 listview的Adapter接口的实现 listview中的MVC 3.知识详解 ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件,这些条目的内容来自于一个ListAdapter .EditText Button TextView ImageView Checkbox 五大布局. 1.布局添加Listvie

Android json数据解析详解及实例代码

Android json数据解析详解 移动开发经常要与服务器数据交互,也常使用json数据格式,那就说说Android json解析. 1.最简单json格式解析如下: //解析json ry { JSONTokener jsonParser = new JSONTokener(strResult); JSONObject jsonObj = (JSONObject) jsonParser.nextValue(); String strsportsTitle = jsonObj.getStrin

Android App增量更新详解及实例代码_Android

Android App增量更新实例--Smart App Updates        介绍 你所看到的,是一个用于Android应用程序增量更新的开源库. 包括客户端.服务端两部分代码. 原理 自从 Android 4.1 开始,Google引入了应用程序的增量更新. Link: http://developer.android.com/about/versions/jelly-bean.html Smart app updates is a new feature of Google Pla

Android listview与adapter详解及实例代码_Android

一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常简单. 一个ListView的创建需要3个元素. (1)ListView中的每一列的View. (2)填入View的数据或者图片等. (3)连接数据与ListView的适配器. 也就是说,要使用ListView,首先要了解什么是适配器.适配器是一个连接数据和AdapterView(ListView就

Android App增量更新详解及实例代码

Android App增量更新实例--Smart App Updates 介绍 你所看到的,是一个用于Android应用程序增量更新的开源库. 包括客户端.服务端两部分代码. 原理 自从 Android 4.1 开始,Google引入了应用程序的增量更新. Link: http://developer.android.com/about/versions/jelly-bean.html Smart app updates is a new feature of Google Play that

Android 自定义gradle property详解及实例代码

Android 自定义gradle property 在Android studio上运行项目,gradle的配置是必不可少的,但是随着项目的逐渐成长,迎面而来的就是.各种依赖包的添加,数不胜数的签名,渠道包等,整个gradle变得很乱,这样其实我们可以将gradle的部分内容分离出来放在另一个自定义gradle内. 如这时我们添加的Plugin 就只要对其赋值就可以了. 步骤: 在总项目根目录下创建 dependencies.gradle文件(名字可以自定义) 根目录下创建的自定义内容如下:

Android ViewPagerIndicator详解及实例代码

Android ViewPagerIndicator详解及实例代码 关于自定义View的属性零碎知识 自定义View和自定义属性的知识不再此提及,这里着重说的是属性在自定义View中的获取方式,自定义的属性如下: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Wisely"> <attr name=&

Android Tab 控件详解及实例

Android Tab 控件详解及实例 在桌面应用中Tab控件使用得非常普遍,那么我们经常在Android中也见到以Tab进行布局的客户端.那么Android中的Tab是如何使用的呢? 1.Activity package com.wicresoft.activity; import com.wicresoft.myandroid.R; import android.app.TabActivity; import android.os.Bundle; import android.util.Lo