苹果的iphone 有语音识别用的是Google 的技术,做为Google 力推的Android 自然会将其核心技术往Android 系统里面植入,并结合google 的云端技术将其发扬光大。

所以Google Voice Recognition在Android 的实现就变得极其轻松。

语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google 提供的Api 实现这一功能。

功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。

功能界面如下:

用户通过点击speak按钮显示界面:

用户说完话后,将提交到云端搜索:

在云端搜索完成后,返回打印数据:

Android 轻松实现语音识别的完整代码

Java代码
  1. *Copyright(C)2008TheAndroidOpenSourceProject
  2. *
  3. *LicensedundertheApacheLicense,Version2.0(the"License");
  4. *youmaynotusethisfileexceptincompliancewiththeLicense.
  5. *YoumayobtainacopyoftheLicenseat
  6. *
  7. *http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  10. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  11. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  12. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  13. *limitationsundertheLicense.
  14. */
  15. packagecom.example.android.apis.app;
  16. importcom.example.android.apis.R;
  17. importandroid.app.Activity;
  18. importandroid.content.Intent;
  19. importandroid.content.pm.PackageManager;
  20. importandroid.content.pm.ResolveInfo;
  21. importandroid.os.Bundle;
  22. importandroid.speech.RecognizerIntent;
  23. importandroid.view.View;
  24. importandroid.view.View.OnClickListener;
  25. importandroid.widget.ArrayAdapter;
  26. importandroid.widget.Button;
  27. importandroid.widget.ListView;
  28. importjava.util.ArrayList;
  29. importjava.util.List;
  30. /**
  31. *SamplecodethatinvokesthespeechrecognitionintentAPI.
  32. */
  33. publicclassVoiceRecognitionextendsActivityimplementsOnClickListener{
  34. privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE=1234;
  35. privateListViewmList;
  36. /**
  37. *Calledwiththeactivityisfirstcreated.
  38. */
  39. @Override
  40. publicvoidonCreate(BundlesavedInstanceState){
  41. super.onCreate(savedInstanceState);
  42. //InflateourUIfromitsXMLlayoutdescription.
  43. setContentView(R.layout.voice_recognition);
  44. //Getdisplayitemsforlaterinteraction
  45. ButtonspeakButton=(Button)findViewById(R.id.btn_speak);
  46. mList=(ListView)findViewById(R.id.list);
  47. //Checktoseeifarecognitionactivityispresent
  48. PackageManagerpm=getPackageManager();
  49. Listactivities=pm.queryIntentActivities(
  50. newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);
  51. if(activities.size()!=0){
  52. speakButton.setOnClickListener(this);
  53. }else{
  54. speakButton.setEnabled(false);
  55. speakButton.setText("Recognizernotpresent");
  56. }
  57. }
  58. /**
  59. *Handletheclickonthestartrecognitionbutton.
  60. */
  61. publicvoidonClick(Viewv){
  62. if(v.getId()==R.id.btn_speak){
  63. startVoiceRecognitionActivity();
  64. }
  65. }
  66. /**
  67. *Fireanintenttostartthespeechrecognitionactivity.
  68. */
  69. privatevoidstartVoiceRecognitionActivity(){
  70. Intentintent=newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  71. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  72. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  73. intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speechrecognitiondemo");
  74. startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
  75. }
  76. /**
  77. *Handletheresultsfromtherecognitionactivity.
  78. */
  79. @Override
  80. protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
  81. if(requestCode==VOICE_RECOGNITION_REQUEST_CODE&&resultCode==RESULT_OK){
  82. //Fillthelistviewwiththestringstherecognizerthoughtitcouldhaveheard
  83. ArrayListmatches=data.getStringArrayListExtra(
  84. RecognizerIntent.EXTRA_RESULTS);
  85. mList.setAdapter(newArrayAdapter(this,android.R.layout.simple_list_item_1,
  86. matches));
  87. }
  88. super.onActivityResult(requestCode,resultCode,data);
  89. }
  90. }

更多相关文章

  1. Android(安卓)获取ROOT权限原理解析
  2. Android(安卓)实现记住用户名和密码的功能
  3. Android(安卓)轻松实现语音识别的完整代码
  4. Android(安卓)用户界面(User Interface)概要
  5. Android身份证件识别的OCR技术SDK
  6. Android技术架构演进与未来
  7. 《Android(安卓)Dev Guide》系列教程10:用户界面之创建菜单
  8. Android(安卓)4.0用户界面设计和元素-ICS简介
  9. Android(安卓)成用户隐私安全问题严重

随机推荐

  1. Android中使用ALSA声卡
  2. Android获取设备ID号
  3. 01——Introduction to Android介绍
  4. Android之拨号器
  5. Android(安卓)游戏开发
  6. Android5.x+ 格式化外部存储(u盘, sdcard
  7. android中log知识总结
  8. LinearLayout和RelativeLayout的属性区别
  9. Android(安卓)透明Dialog
  10. android 自定义组件圆形边框