如何在android平台上实现语音识别
来源:互联网 发布:淘宝十大零食排行榜 编辑:程序博客网 时间:2024/03/29 23:19
作者:张宗硕
谷歌允许开发人员调用他们实现的语音识别的接口,当然识别率不是很高,个人感觉不如科大讯飞做的语音识别率那么高,但通过谷歌给开发的接口实现语音识别是个非常简单的事情。Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,并且要保证手机支持语音识别,否则运行下面的代码会看到找不到语音设备这条提示信息。解决的方法是用手机下载支持语音输入的安装包。
Java代码如下:
package com.iwin.zzs;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.speech.RecognizerIntent;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MyspeakActivity extends Activity {private Button btnReconizer;private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);btnReconizer.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry{//通过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 exceptione.printStackTrace();Toast.makeText(getApplicationContext(), "找不到语音设备", 1).show();}}}); }@Overrideprotected 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);}}
main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:id="@+id/btnRecognizer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试语音识别" /></LinearLayout>
最后,千万不要忘了在AndroidManifest.xml加入下面一句话:
<uses-permissionandroid:name="android.permission.INTERNET" />
其主要实现语音识别的原理就是将用户发出的语音发送到google云端,然后经过云端的处理,匹配到相应的数据,最后发送到客户端。
- 如何在android平台上实现语音识别
- 如何在android平台上实现语音识别
- 如何利用google原生包在android平台上实现语音识别
- 如何利用google原生包在android平台上实现语音识别
- 语音识别技术在树莓派平台上的使用
- 在Android平台上实现条型码扫描与识别
- Android 实现语音识别
- Android实现语音识别
- Android实现语音识别
- Android实现语音识别 .
- Android开发学习之使用百度语音识别SDK实现语音识别(上)
- 如何实现语音识别功能
- 如何实现语音识别功能
- 在android平台上如何实现H264解码
- 如何在Windows phone8 中实现iphone中的siri,实现语音合成与语音识别
- Android 轻松实现语音识别
- Android 轻松实现语音识别
- Android 轻松实现语音识别
- 关于recv()的一种特殊情况处理(多条消息作为了一条接收)
- jquery中各种插件的应用
- Android屏幕大小和密度对照表,以及px、dip、sp等像素单位的解释
- mini2440学习笔记 之01 配置ADS1.2编译调试程序(在sram中点亮LED)
- 职场终极密籍--记我的职业生涯
- 如何在android平台上实现语音识别
- 让数据库变快的10个建议
- icmp (http) 隧道技术
- Animations的使用总结
- Android 显示单位px和dip以及sp的区别
- hadoop集群的配置笔记
- ajax
- Android中SurfaceView的使用示例
- CentOS 6 系统优化 Shell 脚本