如何利用google原生包在android平台上实现语音识别

来源:互联网 发布:剑灵龙族女捏脸数据图 编辑:程序博客网 时间:2024/04/20 20:40
谷歌允许开发人员调用他们实现的语音识别的接口,当然识别率不是很高,个人感觉不如科大讯飞做的语音识别率那么高,但通过谷歌给开发的接口实现语音识别是个非常简单的事情。Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,并且要保证手机支持语音识别,否则运行下面的代码会看到找不到语音设备这条提示信息。解决的方法是用手机下载支持语音输入的安装包。

Java代码如下:

[java] view plaincopyprint?
  1. package com.iwin.zzs;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.speech.RecognizerIntent;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11. public class MyspeakActivity extends Activity {  
  12.   
  13.     private Button btnReconizer;  
  14.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);  
  21.           
  22.         btnReconizer.setOnClickListener(new Button.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 // TODO Auto-generated method stub  
  26.                 try{  
  27.                     //通过Intent传递语音识别的模式,开启语音  
  28.                     Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  29.                     //语言模式和自由模式的语音识别  
  30.                     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  31.                     //提示语音开始  
  32.                     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");  
  33.                     //开始语音识别  
  34.                     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  35.                 }catch (Exception e) {  
  36.                     // TODO: handle exception  
  37.                     e.printStackTrace();  
  38.                     Toast.makeText(getApplicationContext(), "找不到语音设备"1).show();  
  39.                 }  
  40.             }  
  41.         });  
  42.         }  
  43.     @Override  
  44.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  45.         // TODO Auto-generated method stub  
  46.         //回调获取从谷歌得到的数据  
  47.         if(requestCode==VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){  
  48.         //取得语音的字符  
  49.             ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  50.             String resultString="";  
  51.             for(int i=0;i<results.size();i++){  
  52.                 resultString+=results.get(i);  
  53.             }  
  54.             Toast.makeText(this, resultString, 1).show();  
  55.         }  
  56.         super.onActivityResult(requestCode, resultCode, data);  
  57.     }  
  58.     }  
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.iwin.zzs;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.speech.RecognizerIntent;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11. public class MyspeakActivity extends Activity {  
  12.   
  13.     private Button btnReconizer;  
  14.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);  
  21.           
  22.         btnReconizer.setOnClickListener(new Button.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 // TODO Auto-generated method stub  
  26.                 try{  
  27.                     //通过Intent传递语音识别的模式,开启语音  
  28.                     Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  29.                     //语言模式和自由模式的语音识别  
  30.                     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  31.                     //提示语音开始  
  32.                     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");  
  33.                     //开始语音识别  
  34.                     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  35.                 }catch (Exception e) {  
  36.                     // TODO: handle exception  
  37.                     e.printStackTrace();  
  38.                     Toast.makeText(getApplicationContext(), "找不到语音设备"1).show();  
  39.                 }  
  40.             }  
  41.         });  
  42.         }  
  43.     @Override  
  44.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  45.         // TODO Auto-generated method stub  
  46.         //回调获取从谷歌得到的数据  
  47.         if(requestCode==VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){  
  48.         //取得语音的字符  
  49.             ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  50.             String resultString="";  
  51.             for(int i=0;i<results.size();i++){  
  52.                 resultString+=results.get(i);  
  53.             }  
  54.             Toast.makeText(this, resultString, 1).show();  
  55.         }  
  56.         super.onActivityResult(requestCode, resultCode, data);  
  57.     }  
  58.     }  


main.xml代码如下:


[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button    
  8.     android:id="@+id/btnRecognizer"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="测试语音识别"  
  12.     />  
  13. </LinearLayout>  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button    
  8.     android:id="@+id/btnRecognizer"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="测试语音识别"  
  12.     />  
  13. </LinearLayout>  


最后,千万不要忘了在AndroidManifest.xml加入下面一句话:

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

其主要实现语音识别的原理就是将用户发出的语音发送到google云端,然后经过云端的处理,匹配到相应的数据,最后发送到客户端。

0 0
原创粉丝点击