android 语音识别

原文链接:http://maimode.iteye.com/blog/1673384

speech api参考:http://developer.android.com/intl/zh-CN/reference/android/speech/package-summary.html

 

android开发入门参考:http://maimode.iteye.com/blog/1634268

 

下文给出核心的代码部分:

 

EgSpeechActivity(启动的activity)

 

Java代码   收藏代码
  1. package com.example.androideg.speech;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.content.pm.PackageManager;  
  9. import android.content.pm.ResolveInfo;  
  10. import android.os.Bundle;  
  11. import android.speech.RecognizerIntent;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.Button;  
  15.   
  16. public class EgSpeechActivity extends Activity implements OnClickListener {  
  17.      public final static String EXTRA_MESSAGE = "com.example.androideg.speech.MESSAGE";  
  18.        
  19.      private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;    
  20.      private static final String SPEECH_PROMPT = "请讲话";  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         // 检查是否存在recognition activity  
  28.         PackageManager pm = getPackageManager();  
  29.         List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(  
  30.                 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  31.         Button btn = (Button) findViewById(R.id.btn_speek);  
  32.         if (activities.size() != 0) {  
  33.             //如果存在recognition activity则为按钮绑定点击事件  
  34.             btn.setOnClickListener(this);  
  35.         } else {  
  36.             // 如果不存在则禁用按钮  
  37.             btn.setEnabled(false);  
  38.             btn.setText("语音识别不可用");  
  39.         }  
  40.   
  41.     }  
  42.   
  43.     @Override  
  44.     public void onClick(View v) {  
  45.         if (v.getId() == R.id.btn_speek){  
  46.             startVoiceRecognitionActivity();  
  47.         }  
  48.     }  
  49.       
  50.     /** 
  51.      * 启动语音识别activity,接收用户语音输入 
  52.      */  
  53.     private void startVoiceRecognitionActivity(){  
  54.         //通过Intent传递语音识别的模式    
  55.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
  56.         //语言模式:自由形式的语音识别    
  57.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  58.         //提示语音开始    
  59.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, SPEECH_PROMPT);    
  60.         //开始执行我们的Intent、语音识别  并等待返回结果  
  61.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  62.     }  
  63.       
  64.     @Override  
  65.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  66.         // 确定是语音识别activity返回的结果  
  67.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE){  
  68.             // 确定返回结果的状态是成功  
  69.             if (resultCode == RESULT_OK){  
  70.                 // 获取语音识别结果    
  71.                 ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);    
  72.                 startDisplayMessageActivity(matches);  
  73.             }  
  74.         }  
  75.           
  76.         super.onActivityResult(requestCode, resultCode, data);  
  77.     }  
  78.       
  79.     /** 
  80.      * 启动展示activity,显示识别结果 
  81.      * @param message 
  82.      */  
  83.     private void startDisplayMessageActivity(ArrayList<String> strList){  
  84.         Intent intent = new Intent(this, DisplayMessageActivity.class);  
  85.         intent.putExtra(EXTRA_MESSAGE, strList);  
  86.         startActivity(intent);  
  87.     }  
  88. }  

相应的layout文件activity_main.xml:

 

 

Xml代码   收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:orientation="vertical"  
  7.     tools:context=".EgSpeechActivity" >  
  8.   
  9.   
  10.     <Button  
  11.         android:id="@+id/btn_speek"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/button_speak" />  
  15.   
  16. </LinearLayout>  

 

DisplayMessageActivity(用于展示识别结果):

 

 

Java代码   收藏代码
  1. package com.example.androideg.speech;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.ListView;  
  10.   
  11. public class DisplayMessageActivity extends Activity {  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         // 设置布局  
  17.         setContentView(R.layout.activity_display_message);  
  18.           
  19.         // 从intent中获取数据  
  20.         Intent intent = getIntent();  
  21.         ArrayList<String> strList = intent.getStringArrayListExtra(EgSpeechActivity.EXTRA_MESSAGE);  
  22.   
  23.         //找到list,然后赋值  
  24.         ListView list = (ListView) findViewById(R.id.list);  
  25.         list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strList));  
  26.     }  
  27.   
  28. }  
 

 相应的layout文件activity_display_message.xml:

 

 

Xml代码   收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical">  
  5.   
  6.     <ListView  
  7.         android:id="@+id/list"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="0dip"  
  10.         android:layout_weight="1" >  
  11.     </ListView>  
  12.   
  13.  </LinearLayout>  

 

经过测试,需要在网络支持的环境下才能识别,支持中文识别。 

 

附件有本示例的eclipse项目文件。

 

运行界面:

 

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值