android 语音识别接口

来源:互联网 发布:淘宝美工的奖金制度 编辑:程序博客网 时间:2024/04/30 20:38

转载地址:点击打开链接

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

主要代码:

[java] view plaincopy
  1. package com.VoiceRecognition;  
  2. import android.app.Activity;    
  3. import android.content.Intent;    
  4. import android.content.pm.PackageManager;    
  5. import android.content.pm.ResolveInfo;    
  6. import android.os.Bundle;    
  7. import android.speech.RecognizerIntent;    
  8. import android.view.View;    
  9. import android.view.View.OnClickListener;    
  10. import android.widget.ArrayAdapter;    
  11. import android.widget.Button;    
  12. import android.widget.ListView;    
  13. import java.util.ArrayList;    
  14. import java.util.List;    
  15.    
  16. public class VoiceRecognition extends Activity implements OnClickListener {    
  17.         
  18.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;    
  19.         
  20.     private ListView mList;    
  21.    
  22.     /**   
  23.      * Called with the activity is first created.   
  24.      */    
  25.     @Override    
  26.     public void onCreate(Bundle savedInstanceState)     
  27.     {    
  28.         super.onCreate(savedInstanceState);    
  29.    
  30.         setContentView(R.layout.main);    
  31.    
  32.         Button speakButton = (Button) findViewById(R.id.btn_speak);    
  33.             
  34.         mList = (ListView) findViewById(R.id.list);    
  35.    
  36.         // Check to see if a recognition activity is present    
  37.         PackageManager pm = getPackageManager();    
  38.         List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);    
  39.         if (activities.size() != 0)    
  40.         {    
  41.             speakButton.setOnClickListener(this);    
  42.         }    
  43.         else    
  44.         {    
  45.             speakButton.setEnabled(false);    
  46.             speakButton.setText("Recognizer not present");    
  47.         }    
  48.     }    
  49.    
  50.    
  51.     public void onClick(View v)    
  52.     {    
  53.         if (v.getId() == R.id.btn_speak)    
  54.         {    
  55.             startVoiceRecognitionActivity();    
  56.         }    
  57.     }    
  58.    
  59.     private void startVoiceRecognitionActivity()    
  60.     {    
  61.         //通过Intent传递语音识别的模式    
  62.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
  63.         //语言模式和自由形式的语音识别    
  64.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);    
  65.         //提示语音开始    
  66.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");    
  67.         //开始执行我们的Intent、语音识别    
  68.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);    
  69.     }    
  70.    
  71.    
  72.     //当语音结束时的回调函数onActivityResult    
  73.     @Override    
  74.     protected void onActivityResult(int requestCode, int resultCode, Intent data)    
  75.     {    
  76.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK)    
  77.         {    
  78.             // 取得语音的字符    
  79.             ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);    
  80.             mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));    
  81.         }    
  82.    
  83.         super.onActivityResult(requestCode, resultCode, data);    
  84.     }    
  85. }    

布局xml文件如下:
[html] view plaincopy
  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/btn_speak"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     />  
  12.     <ListView  
  13.     android:id="@+id/list"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="wrap_content"  
  16.     />  
  17. </LinearLayout>  

最后要注意的是,因为其主要原理就是将语音发送到google云端,然后云端处理,匹配相应的数据,发送到客户端。 所以不要忘记,在manifest中加入网络访问权限:
[html] view plaincopy
  1. <uses-permission android:name="android.permission.INTERNET" /> 

原创粉丝点击