如何利用google原生包在android平台上实现语音识别
2015-04-07 15:19
771 查看
谷歌允许开发人员调用他们实现的语音识别的接口,当然识别率不是很高,个人感觉不如科大讯飞做的语音识别率那么高,但通过谷歌给开发的接口实现语音识别是个非常简单的事情。Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,并且要保证手机支持语音识别,否则运行下面的代码会看到找不到语音设备这条提示信息。解决的方法是用手机下载支持语音输入的安装包。
Java代码如下:
[java] view
plaincopyprint?
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);
btnReconizer.setOnClickListener(new Button.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);
}
}
[java] view
plaincopyprint?
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);
btnReconizer.setOnClickListener(new Button.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);
}
}
main.xml代码如下:
[html] view
plaincopyprint?
<?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>
[html] view
plaincopyprint?
<?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云端,然后经过云端的处理,匹配到相应的数据,最后发送到客户端。
Java代码如下:
[java] view
plaincopyprint?
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);
btnReconizer.setOnClickListener(new Button.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);
}
}
[java] view
plaincopyprint?
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);
btnReconizer.setOnClickListener(new Button.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);
}
}
main.xml代码如下:
[html] view
plaincopyprint?
<?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>
[html] view
plaincopyprint?
<?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云端,然后经过云端的处理,匹配到相应的数据,最后发送到客户端。
相关文章推荐
- 如何利用google原生包在android平台上实现语音识别
- 如何在android平台上实现语音识别
- 如何在android平台上实现语音识别
- 这个开源项目展示了如何实现一个横跨各种Android平台的音乐播放器,包括手机,平板,汽车,手表,电视等。Google官方推出,跨平台开发必看项目。
- 举例说明如何在android中利用Service来实现后台发送邮件功能
- Android 如何实现google天气
- Android平台下利用zxing实现二维码开发
- 避开Google Voice Search利用Google Speech API实现Android语音识别之Demo实现
- 【iPhone Demo】地图自定义大头针如何利用Android TabHost+ActivityGroup+Broadcast实现类似于iPhone的底部菜单
- 利用Ptrace在Android平台实现应用程序控制
- Android平台下利用zxing实现二维码开发
- Android: 如何利用Handler处理和发送消息来实现页面刷新的功能
- 利用Ptrace在Android平台实现应用程序控制[转]
- Android 如何修改Message里解析表情的行为和Google原生的行为一样
- Android 平台 Button控件如何实现按下效果 (变换风格)
- 在android里如何利用WebView实现和javascript通信
- 利用google开发平台实现按关键词搜索结果的爬取
- 避开Google Voice Search利用Google Speech API实现Android语音识别
- 避开Google Voice Search利用Google Speech API实现Android语音识别
- 【从零单排】利用科大讯飞语音包实现Android语音识别Demo