Android语音识别功能使用
package com.king.android.speical;
import java.util.ArrayList;
import com.king.android.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* 描述:语音识别功能(只有在真机才能测试)
* 作者:Andy.Liu
* 时间: 2012-7-16 上午07:38:27
**/
public class VoiceActivity extends Activity implements OnClickListener{
private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnVoice = (Button) findViewById(R.id.btn_voice);
btnVoice.setVisibility(View.VISIBLE);
btnVoice.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_voice:
doStartVoice();
}
}
/**
*
*TODO:开始语音
*Author:Andy.Liu
*Create Time:2012-7-16 上午07:42:48
*TAG:
*Return:void
*/
private void doStartVoice(){
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(ActivityNotFoundException e){
//找到到语音设备
Toast.makeText(VoiceActivity.this, "没有找到设备", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(VOICE_RECOGNITION_REQUEST_CODE==requestCode&&RESULT_OK ==resultCode){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
StringBuilder sb = new StringBuilder();
for(String voice:results){
sb.append(voice);
}
Toast.makeText(VoiceActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
有些东西会,但不精通,而我现在要做的就是精通,复习回顾,整理自己的框架,举一反三。。。