1. 程式人生 > >Android 輕鬆實現語音識別

Android 輕鬆實現語音識別

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->/*
*Copyright(C)2008TheAndroidOpenSourceProject
*
*LicensedundertheApacheLicense,Version2.0(the"License");
*youmaynotusethisfileexceptincompliancewiththeLicense.
*YoumayobtainacopyoftheLicenseat
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
*Unlessrequiredbyapplicablelaworagreedtoinwriting,software
*distributedundertheLicenseisdistributedonan"ASIS"BASIS,
*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.
*/
@Override
publicvoidonCreate(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();
List
<ResolveInfo>activities=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);
}

/**
*Handletheresultsfromtherecognitionactivity.
*/
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
if(requestCode==VOICE_RECOGNITION_REQUEST_CODE&&resultCode==RESULT_OK){
//FillthelistviewwiththestringstherecognizerthoughtitcouldhaveheardArrayList<String>matches=data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(
newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
matches));
}

super.onActivityResult(requestCode,resultCode,data);
}
}