Android由于有了Google的支持,那么他的语音识别做起来也是比较简单的,主要是调用谷歌的语音识别软件,然后取得他的返回值,谷歌的语音识别有着极其庞大的云中心以及数据库。当然语音识别是存在一个精度问题的,所以谷歌语音识别软件的返回值不是唯一的,因此到时候需要自己做筛选或者处理:package com.ichances.voice;
1 import java.util.ArrayList; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.speech.RecognizerIntent; 7 import android.util.Log; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.TextView; 12 import android.widget.Toast; 13 14 public class VoiceActivity extends Activity { 15 /** Called when the activity is first created. */ 16 private Button btnReconizer; 17 private static final int VOICE_RECOGNITION_REQUEST_CODE =618; 18 private TextView resText; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 // TODO Auto-generated method stub 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.main); 25 btnReconizer = (Button) this.findViewById(R.id.button1); 26 resText = (TextView) findViewById(R.id.restext); 27 btnReconizer.setOnClickListener(new OnClickListener() { 28 @Override 29 public void onClick(View v) { 30 // TODO Auto-generated method stub 31 try { 32 // 通过Intent传递语音识别的模式,开启语音 33 Intent intent = new Intent( 34 RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 35 // 语言模式和自由模式的语音识别 36 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 37 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 38 // 提示语音开始 39 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音"); 40 // 开始语音识别 41 startActivityForResult(intent, 42 VOICE_RECOGNITION_REQUEST_CODE); 43 } catch (Exception e) { 44 // TODO: handle exception 45 e.printStackTrace(); 46 Toast.makeText(getApplicationContext(), "找不到语音设备", 1) 47 .show(); 48 } 49 } 50 }); 51 } 52 53 @Override 54 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 55 // TODO Auto-generated method stub 56 // 回调获取从谷歌得到的数据 57 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE 58 && resultCode == RESULT_OK) { 59 // 取得语音的字符 60 ArrayList<String> results = data 61 .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 62 String resultString = ""; 63 for (int i = 0; i < results.size(); i++) { 64 resultString += String.valueOf(i + 1) + ":" + results.get(i) 65 + ";"; 66 Log.i("结果", resultString); 67 } 68 resText.setText(resultString); 69 Toast.makeText(this, resultString, 1).show(); 70 } 71 super.onActivityResult(requestCode, resultCode, data); 72 } 73 }