Android轻松实现语音识别老枪发布于2010年12月17日8时,56评/71680阅分享到:收藏+236踩顶0苹果的iphone有语音识别用的是Google的技术,做为Google力推的Android自然会将其核心技术往Android系统里面植入,并结合google的云端技术将其发扬光大。所以GoogleVoiceRecognition在Android的实现就变得极其轻松。语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google提供的Api实现这一功能。功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。功能界面如下:用户通过点击speak按钮显示界面:用户说完话后,将提交到云端搜索:在云端搜索完成后,返回打印数据:标签:Android语音识别AndroidSDK代码片段(1)[全屏查看所有代码]1.[代码]Android轻松实现语音识别的完整代码跳至[1][全屏预览]?1*Copyright(C)2008TheAndroidOpenSourceProject23456789101112131415161718192021222324252627282930313233343536373839404142434445**LicensedundertheApacheLicense,Version2.0(theLicense);*youmaynotusethisfileexceptincompliancewiththeLicense.*YoumayobtainacopyoftheLicenseat****Unlessrequiredbyapplicablelaworagreedtoinwriting,software*distributedundertheLicenseisdistributedonanASISBASIS,*WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.*SeetheLicenseforthespecificlanguagegoverningpermissionsand*limitationsundertheLicense.*/packagecom.example.android.apis.app;importcom.example.android.apis.R;importandroid.app.Activity;importandroid.content.Intent;importandroid.content.pm.PackageManager;importandroid.content.pm.ResolveInfo;importandroid.os.Bundle;importandroid.speech.RecognizerIntent;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.ArrayAdapter;importandroid.widget.Button;importandroid.widget.ListView;importjava.util.ArrayList;importjava.util.List;/***SamplecodethatinvokesthespeechrecognitionintentAPI.*/publicclassVoiceRecognitionextendsActivityimplementsOnClickListener{privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE=1234;privateListViewmList;/***Calledwiththeactivityisfirstcreated.4647484950515253545556575859606162636465666768697071727374757677787980818283848586878889*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);//InflateourUIfromitsXMLlayoutdescription.setContentView(R.layout.voice_recognition);//GetdisplayitemsforlaterinteractionButtonspeakButton=(Button)findViewById(R.id.btn_speak);mList=(ListView)findViewById(R.id.list);//ChecktoseeifarecognitionactivityispresentPackageManagerpm=getPackageManager();Listactivities=pm.queryIntentActivities(newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);if(activities.size()!=0){speakButton.setOnClickListener(this);}else{speakButton.setEnabled(false);speakButton.setText(Recognizernotpresent);}}/***Handletheclickonthestartrecognitionbutton.*/publicvoidonClick(Viewv){if(v.getId()==R.id.btn_speak){startVoiceRecognitionActivity();}}/***Fireanintenttostartthespeechrecognitionactivity.*/privatevoidstartVoiceRecognitionActivity(){Intentintent=newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);intent.putExtra(RecognizerIntent.EXTRA_PROMPT,Speechrecognitiondemo);startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);}90919293949596979899100101102103104105106/***Handletheresultsfromtherecognitionactivity.*/@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){if(requestCode==VOICE_RECOGNITION_REQUEST_CODE&&resultCode==RESULT_OK){//FillthelistviewwiththestringstherecognizerthoughtitcouldhaveheardArrayListmatches=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);mList.setAdapter(newArrayAdapter(this,android.R.layout.simple_list_item_1,matches));}super.onActivityResult(requestCode,resultCode,data);}}Android:使用SpeechToTextAPI进行语音到文本转换【南京·10月17日】OSC源创会开始报名:Swift、大型移动项目构架分享»Android有一个非常酷的特性很多开发者都还不知道。Any.DO之类应用的语音到文本转换功能很有创意。在现在Siri的世界里,语音指令是极其重要的。Android原生提供SpeechToText功能,为什么不把它用在我们的程序中!我将会展示如何在程序中使用Android的SpeechToTextAPI,现在开始写我们的demo程序。Demo程序这个程序很简单。他有一个Mic符号按钮。点击之后我们触发Android的SpeechToText意图(Intent)显示一个对话框来接收语音输入。输入的语音然后会被转换成文本并显示到一个textview中。第一步:在Eclipse中创建基本的Android项目在Eclipse中创建一个HelloWorldAndroid项目。打开NewProjectAndroidProject,项目名填SpeechToTextDemo,选择Android运行时2.1或sdk7。我给定了包名:net.viralpatel.android.speechtotextdemo做完上面的步骤,你就有了一个基本的AndroidHelloWorld程序第二步:更改布局在我们的demo中布局很简单。只有一个图像按钮来触发SpeechtoTextAPI和一个TextView来显示从语音转换过来的文本。打开layout/main.xml并替换为下面的内容:File:res/layout/main.xml?1234567891011121314151617181920212223242526272829LinearLayoutxmlns:android=:tools=:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:layout_above=@+id/textView1android:layout_toLeftOf=@+id/textView1android:gravity=centerandroid:orientation=verticalImageButtonandroid:id=@+id/btnSpeakandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:layout_margin=10dpandroid:layout_marginRight=10dpandroid:layout_marginTop=10dpandroid:contentDescription=@string/speakandroid:src=ahref==referertarget=_blank@android/a:drawable/ic_btn_speak_now/TextViewandroid:id=@+id/txtTextandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_marginLeft=10dpandroid:layout_marginR