- 在谷歌應用市場安裝好谷歌語音搜索
- 下載語音離線包
- 調用谷歌語音API
-判斷是否支持谷歌語音識別功能(很多墻內閹割機用不了)
啟動語音識別功能 另一種方法是 採用startActitvityForResult(大同小異,自行搜索)
if(SpeechRecognizer.isRecognitionAvailable(getActivity())){
if(intent==null){
intent=newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//設置支持過程中可用的識別結果
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
//語言模型
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//用於語音搜索的額外密鑰
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getActivity().getPackageName());
//最大結果數
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
sr.startListening(intent);
}else{
if(sr!=null){
sr.startListening(intent);
}
}
//提供語音識別服務的類
privateSpeechRecognizersr;
//實例化
sr=SpeechRecognizer.createSpeechRecognizer(getActivity());
//設置監聽
sr.setRecognitionListener(newListener());
//語音監聽
classListenerimplementsRecognitionListener{
@Override
publicvoidonReadyForSpeech(Bundl eparams){
//endpointer 準備好讓用戶開始說話
}
@Override
publicvoidonBeginningOfSpeech(){
//開始說話
}
@Override
publicvoidonRmsChanged(floatrmsdB){
//音頻聲級更改時調用.不保證一定會成功調用
}
@Override
publicvoidonBufferReceived(byte[]buffer){
//收到更多的聲音時,不保證一定調用
}
@Override
publicvoidonEndOfSpeech(){
//結束說話
}
@Override
publicvoidonError(interror){
//異常
}
@Override
publicvoidonResults(Bundleresults){
//最終結果
}
@Override
publicvoidonPartialResults(BundlepartialResults){
//intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);設置后這裡就可以獲取過程中的識別結果
}
@Override
publicvoidonEvent(inteventType,Bundleparams){
//保留,用於添加未來事件
}
}
Starts an activity that will prompt the user for speech and send it through a speech recognizer.
Optional IETF language tag (as defined by BCP 47), for example "en-US".
Informs the recognizer which speech model to prefer when performing ACTION_RECOGNIZE_SPEECH.
Optional boolean, to be used with ACTION_RECOGNIZE_SPEECH,ACTION_VOICE_SEARCH_HANDS_FREE, ACTION_WEB_SEARCH to indicate whether to only use an offline speech recognition engine.
Result code returned when a network error was encountered
等等
以下鏈接非常詳細的介紹了各API的用法
https://developer.android.com/reference/android/speech/RecognitionListener
https://developer.android.com/reference/android/speech/RecognizerIntent
https://developer.android.com/reference/android/speech/SpeechRecognizer
百度的時候千篇一律 RecognizerIntent的講解很少最後還是看的官方文檔
掌握科學上網很重要,官方文檔講解非常全面