android上实现语音识别,基于google的语音识的简单例子.

来源:互联网 发布:炉石传说淘宝卡包 编辑:程序博客网 时间:2024/04/20 11:39

语音识别在android上使用起来很方便也很简单.

但是有个前提条件,就是android机器上必须预先安装google的语音搜索工具

语音识别技术是在Android SDK1.5中才加入的(RecognizerIntent),这里我们简单的分析一下自带的api例子,其实它就是通过一个Intent的Action动作来完成的。主要有以下两种模式:

ACTION_RECOGNIZE_SPEECH:一般语音识别,在这种模式下我们可以捕捉到语音的处理后的文字列。

ACTION_WEB_SEARCH:网络搜索

该例子同样是使用ACTION_RECOGNIZE_SPEECH模式,我们需要实现onActivityResult方法,当语音识别结束之后的回调函数。


下面是核心代码:

[java] view plaincopyprint?
  1. package com.ifeisu.test.voice;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.content.pm.PackageManager;  
  8. import android.content.pm.ResolveInfo;  
  9. import android.os.Bundle;  
  10. import android.speech.RecognizerIntent;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.Button;  
  15. import android.widget.ListView;  
  16. /*** 
  17.  * 语音识别demo,在使用此demo之前需要确认手机或者模拟器上安装了google的语音搜索工具 
  18.  * Voice_Search_2.1.4.apk或者更高版本 
  19.  * @author guoxinzz@163.com 
  20.  * @website http://www.ifeisu.com 
  21.  * 
  22.  */  
  23. public class VoiceRecognition extends Activity implements OnClickListener {  
  24.   
  25.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  26.   
  27.     private ListView mList;  
  28.   
  29.     /** 
  30.      *  
  31.      * Called with the activity is first created. 
  32.      */  
  33.   
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState)  
  36.   
  37.     {  
  38.   
  39.         super.onCreate(savedInstanceState);  
  40.   
  41.         setContentView(R.layout.main);  
  42.   
  43.         Button speakButton = (Button) findViewById(R.id.btn_speak);  
  44.   
  45.         mList = (ListView) findViewById(R.id.list);  
  46.   
  47.         // Check to see if a recognition activity is present  
  48.   
  49.         PackageManager pm = getPackageManager();  
  50.   
  51.         List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(  
  52.                 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  53.   
  54.         if (activities.size() != 0)  
  55.   
  56.         {  
  57.   
  58.             speakButton.setOnClickListener(this);  
  59.   
  60.         }  
  61.   
  62.         else  
  63.   
  64.         {  
  65.   
  66.             speakButton.setEnabled(false);  
  67.   
  68.             speakButton.setText("Recognizer not present");  
  69.   
  70.         }  
  71.   
  72.     }  
  73.   
  74.     public void onClick(View v)  
  75.   
  76.     {  
  77.   
  78.         if (v.getId() == R.id.btn_speak)  
  79.   
  80.         {  
  81.   
  82.             startVoiceRecognitionActivity();  
  83.   
  84.         }  
  85.   
  86.     }  
  87.   
  88.     private void startVoiceRecognitionActivity()  
  89.   
  90.     {  
  91.   
  92.         // 通过Intent传递语音识别的模式  
  93.   
  94.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  95.         // 语言模式和自由形式的语音识别  
  96.   
  97.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
  98.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  99.   
  100.         // 提示语音开始  
  101.   
  102.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT,  
  103.                 "请说出需要打开的网站名字.");  
  104.   
  105.         // 开始执行我们的Intent、语音识别  
  106.   
  107.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  108.   
  109.     }  
  110.   
  111.     // 当语音结束时的回调函数onActivityResult  
  112.   
  113.     @Override  
  114.     protected void onActivityResult(int requestCode, int resultCode, Intent data)  
  115.   
  116.     {  
  117.   
  118.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE  
  119.                 && resultCode == RESULT_OK)  
  120.   
  121.         {  
  122.   
  123.             // 取得语音的字符  
  124.   
  125.             ArrayList<String> matches = data  
  126.                     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  127.   
  128.             mList.setAdapter(new ArrayAdapter<String>(this,  
  129.                     android.R.layout.simple_list_item_1, matches));  
  130.   
  131.         }  
  132.   
  133.         super.onActivityResult(requestCode, resultCode, data);  
  134.   
  135.     }  
  136.   
  137. }  

1.如果手机不支持语音搜索,则需要下载google的语音搜索工具包 Voice_Search_2.1.4.apk,这个可以到hiapk.com或者其他应用商店下载到.

2.源码下载:demo.voice_recognition.rar  因为csdn不让上传附件,在连接上右键->连接另存为,下载回去将名字改为demo.voice_recognition.rar就能正常打开了.

0 0
原创粉丝点击