本文介绍使用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 activityPackageManager 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("语音识别不可用");}    }@Overridepublic 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);}@Overrideprotected 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项目文件。

运行界面:

更多相关文章

  1. Android中ExpandableListView的使用
  2. Android如何获取SIM卡信息
  3. Android(安卓)编程下判断当前设备是手机还是平板
  4. 反编译并修复过的手写识别类--(android 搜狗 libhanwonhw_v15)
  5. Android(安卓)编程下 Touch 事件的分发和消费机制
  6. Android
  7. 细数Android(安卓)Studio中使用junit4测试框架中的坑
  8. Android内容提供者源码
  9. android 单元测试

随机推荐

  1. Android模块编译:m/mm/mmm命令
  2. Android(安卓)O(8.0)通知栏适配
  3. Android动画学习笔记-Android(安卓)Anima
  4. Android(安卓)XmlPullParser 解析xml
  5. 整理出15个Android很有用的代码片段
  6. 总结Android多分辨率支持
  7. android 所有焦点问题汇总【Focus】
  8. arcgis for Android(安卓)100.2 加载shp
  9. Android(安卓)开发实践 ViewGroup 实现左
  10. Design Widget