Android sdk提供了語音識別的接口,有些人在網上找了例子發現運行不了(PS:網上的例子基本就那一個,都是各種轉載的),原因在於手機沒有安裝google語音搜索軟件!去網上下載一個安上就可以了,另外需保持手機網絡暢通。
第一種方法:這種方法會顯示一個語音對話框,各種提示信息會顯示的比較清晰,也是實現起來最簡單的。
觸發語音識別是調用
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "您的語音將轉化為文字");
- startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
VOICE_RECOGNITION_REQUEST_CODE這個是一個靜態的全局變量
sdk解釋是:requestCode If>= 0, this code will be returned in onActivityResult() when the activity exits.
然後復寫onActivityResult()方法:
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){
- ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- Toast.makeText(MainActivity.this, matches.get(0), Toast.LENGTH_LONG).show();
- //to do sth
- }
-
- super.onActivityResult(requestCode, resultCode, data);
- }
第2中方法:這種方法是不顯示對話框,完全在後台運行,給用戶的體驗會更好一些。
初始化:
- SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this);
- recognizer.setRecognitionListener(new listener());
觸發語音識別時調用
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra("calling_package", "VoiceIME");
- intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
- recognizer.startListening(intent);
區別於第一種方法,這種方法要實現一個接口RecognitionListener類
- class listener implements RecognitionListener {
- public void onReadyForSpeech(Bundle params) {
-
- }
-
- public void onBeginningOfSpeech() {
-
- }
-
- public void onRmsChanged(float rmsdB) {
-
- }
-
- public void onBufferReceived(byte[] buffer) {
-
- }
-
- public void onEndOfSpeech() {
-
- }
-
- public void onError(int error) {
- String s = "";
- switch(error){
- case SpeechRecognizer.ERROR_AUDIO:
- s = "錄音設別錯誤";
- break;
- case SpeechRecognizer.ERROR_CLIENT:
- s = "其他客戶端錯誤";
- break;
- case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
- s = "權限不足";
- break;
- case SpeechRecognizer.ERROR_NETWORK:
- s = "網絡連接錯誤";
- break;
- case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
- s = "網絡連接超時";
- break;
- case SpeechRecognizer.ERROR_NO_MATCH:
- s = "沒有匹配項";
- break;
- case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
- s = "識別服務繁忙";
- break;
- case SpeechRecognizer.ERROR_SERVER:
- s = "識別服務器錯誤";
- break;
- case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
- s = "無語音輸入";
- break;
- }
- s += " 請重試";
- Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
-
- }
-
- public void onResults(Bundle results) {
-
- recognizer_result = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION).get(0);
- Toast.makeText(MainActivity.this, recognizer_result, Toast.LENGTH_SHORT).show();
-
- }
-
- public void onPartialResults(Bundle partialResults) {
-
- }
-
- public void onEvent(int eventType, Bundle params) {
-
- }
-
- }
看方法的名字相信大家應該就知道要干什麼了,多看文檔。起初我覺得用第2中方法給用戶的提示信息太少,往往出錯了,用戶不知道是怎麼回事。後面看文檔才發現google android sdk已經定義了很多錯誤的錯誤號,只要根據不同錯誤給出恰當的提示就好了!