Android语音识别技术详解及实例代码
作者:wangkuifeng0118 时间:2022-09-23 03:46:33
今天从网上找了个例子实现了语音识别,个人感觉挺好玩的,就把代码贴出来与大家分享下:
Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别!
下面是RecognizerIntentActivity中的代码:
public class RecognizerIntentActivity extends Activity {
private Button btnReconizer;
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reconizer);
btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);
btnReconizer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//通过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);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(getApplicationContext(), "找不到语音设备", 1).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//回调获取从谷歌得到的数据
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, 1).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
其主要原理就是将语音发送到google云端,然后云端处理,匹配相应的数据,发送到客户端。
最后不要忘记,在manifest中加入网络访问权限:
<uses-permission android:name="android.permission.INTERNET" />
运行后效果:
点击开始语音按钮,然后开始说话(这里要保证手机的网路是打开的):
正在等待云端数据,由于我这是2G的卡,等了好长时间还是加载不下来,等回公司了用公司的wifi试试,如果得到云端数据,就会通过Toast方式打印出来的。
标签:Android,语音识别技术
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java多线程Future和Callable类示例分享
2021-09-02 09:49:37
Android 动画之TranslateAnimation应用详解
2023-06-27 06:17:59
![](https://img.aspxhome.com/file/2023/1/82411_0s.png)
C# this关键字的四种用法
2022-10-06 07:09:22
![](https://img.aspxhome.com/file/2023/5/75595_0s.jpg)
Spring Cloud 配置中心内容加密的配置方法
2023-02-11 09:05:15
Struts2拦截器 关于解决登录的问题
2023-07-02 14:06:09
![](https://img.aspxhome.com/file/2023/1/78241_0s.png)
Spring boot配置文件加解密详解
2023-11-12 00:17:29
![](https://img.aspxhome.com/file/2023/3/59583_0s.jpg)
javaweb上传下载实例完整版解析(上)
2021-07-31 05:40:23
![](https://img.aspxhome.com/file/2023/4/63114_0s.jpg)
Mybatis结果集自动映射的实例代码
2023-07-09 02:13:58
Java为什么基本数据类型不需要进行创建对象?
2022-03-16 08:59:03
Java Socket使用加密协议进行传输对象的方法
2023-11-28 12:47:44
Android编程获取GPS数据的方法详解
2023-09-20 16:37:34
![](https://img.aspxhome.com/file/2023/1/88721_0s.jpg)
关于Scanner对象的输入结束标记问题
2022-02-20 08:02:11
![](https://img.aspxhome.com/file/2023/8/77368_0s.png)
java文件处理工具类详解
2022-12-19 22:49:59
Collections.shuffle()方法实例解析
2021-09-17 18:51:38
如何用C#实现SAGA分布式事务
2022-11-29 20:34:49
![](https://img.aspxhome.com/file/2023/2/67552_0s.png)
java实现表单必填参数验证的方法
2023-06-16 21:41:24
使用springboot整合RateLimiter限流过程
2022-09-12 21:42:48
![](https://img.aspxhome.com/file/2023/5/61655_0s.png)
SpringBoot使用Redis实现分布式锁
2021-11-06 20:10:22
android图片处理 让图片变成圆形
2023-10-03 00:47:30
![](https://img.aspxhome.com/file/2023/6/83536_0s.jpg)
使用@Order控制配置类/AOP/方法/字段的加载顺序详解
2023-09-05 17:34:35
![](https://img.aspxhome.com/file/2023/1/58421_0s.png)