• android语音识别方法一:使用intent调用语音识别程序

1.

说明
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。

2.
本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java

3.
可从此处下载可独立运行的代码:
4.
核心代码及说明

Java代码
  1. packagecom.android.mystt1;
  2. importandroid.app.Activity;
  3. importandroid.content.Intent;
  4. importandroid.content.pm.PackageManager;
  5. importandroid.content.pm.ResolveInfo;
  6. importandroid.os.Bundle;
  7. importandroid.speech.RecognizerIntent;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.ArrayAdapter;
  11. importandroid.widget.Button;
  12. importandroid.widget.ListView;
  13. importjava.util.ArrayList;
  14. importjava.util.List;
  15. publicclassMyStt1ActivityextendsActivityimplementsOnClickListener{
  16. privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE=1234;
  17. privateListViewmList;//显示识别后字串的list控件
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. ButtonspeakButton=(Button)findViewById(R.id.btn_speak);//识别按钮
  23. mList=(ListView)findViewById(R.id.list);
  24. PackageManagerpm=getPackageManager();
  25. Listactivities=pm.queryIntentActivities(
  26. newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);//本地识别程序
  27. //newIntent(RecognizerIntent.ACTION_WEB_SEARCH),0);//网络识别程序
  28. if(activities.size()!=0){
  29. speakButton.setOnClickListener(this);
  30. }else{//若检测不到语音识别程序在本机安装,测将扭铵置灰
  31. speakButton.setEnabled(false);
  32. speakButton.setText("Recognizernotpresent");
  33. }
  34. }
  35. publicvoidonClick(Viewv){
  36. if(v.getId()==R.id.btn_speak){
  37. startMysttActivityActivity();
  38. }
  39. }
  40. privatevoidstartMysttActivityActivity(){//开始识别
  41. Intentintent=newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  42. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  43. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  44. intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speechrecognitiondemo");
  45. startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
  46. //调出识别界面
  47. }
  48. @Override
  49. protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
  50. if(requestCode==VOICE_RECOGNITION_REQUEST_CODE&&resultCode==RESULT_OK){
  51. //Fillthelistviewwiththestringstherecognizerthoughtitcouldhaveheard
  52. ArrayListmatches=data.getStringArrayListExtra(
  53. RecognizerIntent.EXTRA_RESULTS);
  54. mList.setAdapter(newArrayAdapter(this,android.R.layout.simple_list_item_1,
  55. matches));
  56. }
  57. //语音识别后的回调,将识别的字串在list中显示
  58. super.onActivityResult(requestCode,resultCode,data);
  59. }
  60. }
  • android语音识别方法二:应用程序自己调用语音识别库

1.
说明
以下例程功能为:应用程序自身调用语言识别函数,程序以循环方式等待录音并识别后的字串。
2.
本例参考自android代码:
frameworks/base/core/java/android/speech/srec/Recognizer.java中注释部分
3.
可从此处下载可独立运行的代码:代码在一楼
4.
核心代码及说明

Java代码
  1. packagecom.android.mystt2;
  2. importandroid.app.Activity;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. importandroid.widget.Button;
  6. importandroid.widget.TextView;
  7. importandroid.view.View;
  8. importandroid.view.View.OnClickListener;
  9. importandroid.speech.srec.Recognizer;
  10. importandroid.speech.srec.MicrophoneInputStream;
  11. importjava.io.InputStream;
  12. importjava.io.IOException;
  13. importandroid.util.Log;
  14. publicclassMyStt2ActivityextendsActivityimplementsOnClickListener{
  15. privateTextViewmText;
  16. privatestaticfinalStringTAG="MyStt3Activity";
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. ButtonspeakButton=(Button)findViewById(R.id.btn_speak);//识别扭按
  22. mText=(TextView)findViewById(R.id.text);//显示识别后的字串
  23. speakButton.setOnClickListener(this);
  24. }
  25. publicvoidonClick(Viewv){
  26. if(v.getId()==R.id.btn_speak){
  27. test();
  28. }
  29. }
  30. voidtest(){
  31. try{
  32. InputStreamaudio=newMicrophoneInputStream(11025,11025*5);//设置输入参数
  33. Stringcdir=Recognizer.getConfigDir(null);//获取语音识别配置目录
  34. Recognizerrecognizer=newRecognizer(cdir+"/baseline11k.par");
  35. Recognizer.Grammargrammar=recognizer.newGrammar(cdir
  36. +"/grammars/VoiceDialer.g2g");
  37. grammar.setupRecognizer();
  38. grammar.resetAllSlots();
  39. grammar.compile();
  40. recognizer.start();//开始识别
  41. while(true){//循环等待识别结果
  42. switch(recognizer.advance()){
  43. caseRecognizer.EVENT_INCOMPLETE:
  44. caseRecognizer.EVENT_STARTED:
  45. caseRecognizer.EVENT_START_OF_VOICING:
  46. caseRecognizer.EVENT_END_OF_VOICING:
  47. continue;//未完成,继续等待识别结果
  48. caseRecognizer.EVENT_RECOGNITION_RESULT:
  49. for(inti=0;i<recognizer.getResultCount();i++){
  50. Stringresult=recognizer.getResult(i,
  51. Recognizer.KEY_LITERAL);
  52. Log.d(TAG,"result"+result);
  53. mText.setText(result);
  54. }//识别到字串,显示并退出循环
  55. break;
  56. caseRecognizer.EVENT_NEED_MORE_AUDIO:
  57. recognizer.putAudio(audio)//需要更多音频数据;
  58. continue;
  59. default:
  60. break;
  61. }
  62. break;
  63. }
  64. recognizer.stop();
  65. recognizer.destroy();
  66. audio.close();//回收资源
  67. }catch(IOExceptione){
  68. Log.d(TAG,"error",e);
  69. mText.setText("error"+e);
  70. }
  71. }
  72. }
  • 语音识别方法三:使用Service调用语音识别程序

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.
可从此处下载可独立运行的代码(前台程序):源代码在一楼
4.
核心代码及说明

Java代码
  1. packagecom.android.mystt3;
  2. importandroid.app.Activity;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.View.OnClickListener;
  7. importandroid.speech.RecognitionListener;
  8. importandroid.speech.RecognizerIntent;
  9. importandroid.speech.SpeechRecognizer;
  10. importandroid.widget.Button;
  11. importandroid.widget.TextView;
  12. importjava.util.ArrayList;
  13. importandroid.util.Log;
  14. publicclassMyStt3ActivityextendsActivityimplementsOnClickListener{
  15. privateTextViewmText;
  16. privateSpeechRecognizersr;
  17. privatestaticfinalStringTAG="MyStt3Activity";
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. ButtonspeakButton=(Button)findViewById(R.id.btn_speak);//识别按钮
  23. mText=(TextView)findViewById(R.id.text);//显示识别字串
  24. speakButton.setOnClickListener(this);
  25. sr=SpeechRecognizer.createSpeechRecognizer(this);//初始化识别工具,得到句柄
  26. sr.setRecognitionListener(newlistener());//注册回调类及函数
  27. }
  28. classlistenerimplementsRecognitionListener//回调类的实现
  29. {
  30. publicvoidonReadyForSpeech(Bundleparams)
  31. {
  32. Log.d(TAG,"onReadyForSpeech");
  33. }
  34. publicvoidonBeginningOfSpeech()
  35. {
  36. Log.d(TAG,"onBeginningOfSpeech");
  37. }
  38. publicvoidonRmsChanged(floatrmsdB)
  39. {
  40. Log.d(TAG,"onRmsChanged");
  41. }
  42. publicvoidonBufferReceived(byte[]buffer)
  43. {
  44. Log.d(TAG,"onBufferReceived");
  45. }
  46. publicvoidonEndOfSpeech()
  47. {
  48. Log.d(TAG,"onEndofSpeech");
  49. }
  50. publicvoidonError(interror)
  51. {
  52. Log.d(TAG,"error"+error);
  53. mText.setText("error"+error);
  54. }
  55. publicvoidonResults(Bundleresults)//返回识别到的数据
  56. {
  57. Stringstr=newString();
  58. Log.d(TAG,"onResults"+results);
  59. ArrayListdata=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  60. for(inti=0;i<data.size();i++)
  61. {
  62. Log.d(TAG,"result"+data.get(i));
  63. str+=data.get(i);
  64. }
  65. mText.setText(str);//显示被识别的数据
  66. }
  67. publicvoidonPartialResults(BundlepartialResults)
  68. {
  69. Log.d(TAG,"onPartialResults");
  70. }
  71. publicvoidonEvent(inteventType,Bundleparams)
  72. {
  73. Log.d(TAG,"onEvent"+eventType);
  74. }
  75. }
  76. publicvoidonClick(Viewv){
  77. if(v.getId()==R.id.btn_speak){
  78. sr.startListening(newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
  79. }
  80. }
  81. }

更多相关文章

  1. 【Android(安卓)开发】: Android(安卓)消息处理机制之三: Handle
  2. 通过android代码获取android系统的imei、手机型号、手机品牌、an
  3. Android应用程序启动过程源代码分析
  4. Android(安卓)学习笔记——利用JNI技术在Android中调用、调试C++
  5. 获取Android(安卓)SDK 源代码并在Eclipse中关联查看的方法--转
  6. Android学习之ListView使用基础
  7. android经典优化点
  8. Android获取所有安装APP信息的详细代码
  9. 判断客户端类型

随机推荐

  1. Retrofit HTTP body返回为空的情况报错 E
  2. android 判断3G WIFI网络
  3. [Android]Nuance SREC native engine vs
  4. 设置默认来电铃声 android
  5. Android 小知识点
  6. Ubuntu下Eclipse开发Android经常崩溃的解
  7. Android HTTP 压缩 gzip
  8. Android 各种Span示例
  9. Android用Apache HttpClient 实现POST和G
  10. Android Monkey