如何在android平臺上實現語音識別

作者:張宗碩

       谷歌允許開發人員調用他們實現的語音識別的接口,當然識別率不是很高,個人感覺不如科大訊飛做的語音識別率那麼高,但通過谷歌給開發的接口實現語音識別是個非常簡單的事情。Android中主要通過RecognizerIntent來實現語音識別,其實代碼比較簡單,但是如果找不到設置,就會拋出異常ActivityNotFoundException,所以我們需要捕捉這個異常。而且語音識別在模擬器上是無法測試的,因爲語音識別是訪問google雲端數據,所以如果手機的網絡沒有開啓,就無法實現識別聲音的!一定要開啓手機的網絡,並且要保證手機支持語音識別,否則運行下面的代碼會看到找不到語音設備這條提示信息。解決的方法是用手機下載支持語音輸入的安裝包。

Java代碼如下:

[java] view plaincopy
  1. package com.iwin.zzs;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.speech.RecognizerIntent;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11. public class MyspeakActivity extends Activity {  
  12.   
  13.     private Button btnReconizer;  
  14.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);  
  21.           
  22.         btnReconizer.setOnClickListener(new Button.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 // TODO Auto-generated method stub  
  26.                 try{  
  27.                     //通過Intent傳遞語音識別的模式,開啓語音  
  28.                     Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  29.                     //語言模式和自由模式的語音識別  
  30.                     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  31.                     //提示語音開始  
  32.                     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "開始語音");  
  33.                     //開始語音識別  
  34.                     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  35.                 }catch (Exception e) {  
  36.                     // TODO: handle exception  
  37.                     e.printStackTrace();  
  38.                     Toast.makeText(getApplicationContext(), "找不到語音設備"1).show();  
  39.                 }  
  40.             }  
  41.         });  
  42.         }  
  43.     @Override  
  44.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  45.         // TODO Auto-generated method stub  
  46.         //回調獲取從谷歌得到的數據  
  47.         if(requestCode==VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){  
  48.         //取得語音的字符  
  49.             ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  50.             String resultString="";  
  51.             for(int i=0;i<results.size();i++){  
  52.                 resultString+=results.get(i);  
  53.             }  
  54.             Toast.makeText(this, resultString, 1).show();  
  55.         }  
  56.         super.onActivityResult(requestCode, resultCode, data);  
  57.     }  
  58.     }  


main.xml代碼如下:


[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button    
  8.     android:id="@+id/btnRecognizer"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="測試語音識別"  
  12.     />  
  13. </LinearLayout>  


最後,千萬不要忘了在AndroidManifest.xml加入下面一句話:

<uses-permissionandroid:name="android.permission.INTERNET" />

其主要實現語音識別的原理就是將用戶發出的語音發送到google雲端,然後經過雲端的處理,匹配到相應的數據,最後發送到客戶端。

發佈了120 篇原創文章 · 獲贊 14 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章