织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

android语音识别方法三:使用Service调用语音识别程序

落鹤生 发布于 2013-02-28 10:48 点击:次 
以下例程功能为:在应用程序中使用通于访问service调用语言识别功能,录音并识别后将识别的字串通过Listener返回给应用程序。注意:使用前需要安装语音识别服务,如编译安装源码中的development/samples/VoiceRecogitionService。
TAG:

1.     说明
以下例程功能为:在应用程序中使用通于访问service调用语言识别功能,录音并识别后将识别的字串通过Listener返回给应用程序。注意:使用前需要安装语音识别服务,如编译安装源码中的development/samples/VoiceRecogitionService。

2.     本例参考自android源码

a)          后台服务
参见development/samples/VoiceRecognitionService/*
此处实现了一个模拟的后台服务,它并未实现真的语音识别,而只是一个框架以示例,编译并安装它,即可在设置的语音输入与输出中看到它,它包含了一个设置界面,当连接这个Service时,如果设置了Letters,则直接返回abc,如果设置了Numbers,则直接返回123
你可以自己实现,用于连接android源码自带的识别引擎srec.

b)         前台程序
参见frameworks/base/core/java/android/speech/Recognition*
它与后台Service交互,此段代码实现在应用程序界面中

3.     可从此处下载可独立运行的代码(前台程序):
http://download.csdn.net/source/2591401

4.     核心代码及说明

  1. package com.android.mystt3; 
  2.  
  3. import android.app.Activity; 
  4. import android.content.Intent; 
  5. import android.os.Bundle; 
  6. import android.view.View; 
  7. import android.view.View.OnClickListener; 
  8. import android.speech.RecognitionListener; 
  9. import android.speech.RecognizerIntent; 
  10. import android.speech.SpeechRecognizer; 
  11. import android.widget.Button; 
  12. import android.widget.TextView; 
  13. import java.util.ArrayList; 
  14. import android.util.Log; 
  15.   
  16. public class MyStt3Activity extends Activity implements OnClickListener { 
  17.        private TextView mText; 
  18.        private SpeechRecognizer sr; 
  19.        private static final String TAG = "MyStt3Activity"
  20.        @Override 
  21.        public void onCreate(Bundle savedInstanceState) { 
  22.                 super.onCreate(savedInstanceState); 
  23.                 setContentView(R.layout.main); 
  24. Button speakButton = (Button) findViewById(R.id.btn_speak);     // 识别按钮 
  25. mText = (TextView) findViewById(R.id.text);      // 显示识别字串 
  26. speakButton.setOnClickListener(this); 
  27. sr = SpeechRecognizer.createSpeechRecognizer(this);        // 初始化识别工具,得到句柄 
  28. sr.setRecognitionListener(new listener());         // 注册回调类及函数 
  29.        } 
  30.  
  31.       class listener implements RecognitionListener            // 回调类的实现 
  32.        { 
  33.                 public void onReadyForSpeech(Bundle params) 
  34.                 { 
  35.                          Log.d(TAG, "onReadyForSpeech"); 
  36.                 } 
  37.                 public void onBeginningOfSpeech() 
  38.                 { 
  39.                          Log.d(TAG, "onBeginningOfSpeech"); 
  40.                 } 
  41.                 public void onRmsChanged(float rmsdB) 
  42.                 { 
  43.                          Log.d(TAG, "onRmsChanged"); 
  44.                 } 
  45.                 public void onBufferReceived(byte[] buffer) 
  46.                 { 
  47.                          Log.d(TAG, "onBufferReceived"); 
  48.                 } 
  49.                 public void onEndOfSpeech() 
  50.                 { 
  51.                          Log.d(TAG, "onEndofSpeech"); 
  52.                 } 
  53.                 public void onError(int error) 
  54.                 { 
  55.                          Log.d(TAG,  "error " +  error); 
  56.                          mText.setText("error " + error); 
  57.                 } 
  58.  
  59.                 public void onResults(Bundle results)     // 返回识别到的数据 
  60.                 { 
  61.                          String str = new String(); 
  62.                          Log.d(TAG, "onResults " + results); 
  63. ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
  64.                          for (int i = 0; i < data.size(); i++) 
  65.                          { 
  66.                                    Log.d(TAG, "result " + data.get(i)); 
  67.                                    str += data.get(i); 
  68.                          } 
  69.                          mText.setText(str);        // 显示被识别的数据 
  70.                 } 
  71.                 public void onPartialResults(Bundle partialResults) 
  72.                 { 
  73.                          Log.d(TAG, "onPartialResults"); 
  74.                 } 
  75.                 public void onEvent(int eventType, Bundle params) 
  76.                 { 
  77.                          Log.d(TAG, "onEvent " + eventType); 
  78.                 } 
  79.        } 
  80.        public void onClick(View v) { 
  81.                 if (v.getId() == R.id.btn_speak) { 
  82. sr.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)); 
  83.                 } 
  84.        } 

(转载请注明出处: http://xy0811.spaces.live.com/

(xy0811)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201302/16523.html]
本文出处:live 作者:xy0811 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容