Android 语音识别示例
本文介绍使用android.speech包下的api实现一个简单的语音识别例子。
speech api参考:http://developer.android.com/intl/zh-CN/reference/android/speech/package-summary.html
android开发入门参考:http://maimode.iteye.com/blog/1634268
下文给出核心的代码部分:
EgSpeechActivity(启动的activity)
package com.example.androideg.speech;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EgSpeechActivity extends Activity implements OnClickListener {
public final static String EXTRA_MESSAGE = "com.example.androideg.speech.MESSAGE";
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
private static final String SPEECH_PROMPT = "请讲话";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 检查是否存在recognition activity
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
Button btn = (Button) findViewById(R.id.btn_speek);
if (activities.size() != 0) {
//如果存在recognition activity则为按钮绑定点击事件
btn.setOnClickListener(this);
} else {
// 如果不存在则禁用按钮
btn.setEnabled(false);
btn.setText("语音识别不可用");
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_speek){
startVoiceRecognitionActivity();
}
}
/**
* 启动语音识别activity,接收用户语音输入
*/
private void startVoiceRecognitionActivity(){
//通过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, SPEECH_PROMPT);
//开始执行我们的Intent、语音识别 并等待返回结果
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 确定是语音识别activity返回的结果
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE){
// 确定返回结果的状态是成功
if (resultCode == RESULT_OK){
// 获取语音识别结果
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
startDisplayMessageActivity(matches);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 启动展示activity,显示识别结果
* @param message
*/
private void startDisplayMessageActivity(ArrayList<String> strList){
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, strList);
startActivity(intent);
}
}
相应的layout文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".EgSpeechActivity" > <Button android:id="@+id/btn_speek" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button_speak" /> </LinearLayout>
DisplayMessageActivity(用于展示识别结果):
package com.example.androideg.speech;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置布局
setContentView(R.layout.activity_display_message);
// 从intent中获取数据
Intent intent = getIntent();
ArrayList<String> strList = intent.getStringArrayListExtra(EgSpeechActivity.EXTRA_MESSAGE);
//找到list,然后赋值
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strList));
}
}
相应的layout文件activity_display_message.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > </ListView> </LinearLayout>
经过测试,需要在网络支持的环境下才能识别,支持中文识别。
附件有本示例的eclipse项目文件。
运行界面: