android離線語音識別demo

開始做這個的時候,從網上當了一段代碼,但後來測試老是提示沒有找到設備。。。非常煩人。。。

經過多方查找資料,發現需要裝一個Google語音的插件,運行語音識別的時候要用到。如果沒有就提示沒有找到設備。

下載地址:http://download.csdn.net/detail/wojiao555555/6014985

代碼如下:

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


上邊的那個需要連網,因爲是把語音信號發送到google服務器上進行比對、識別的。所以這次打算弄個離線的demo,就是開着航班模式也能識別的。demo。

參考如下:

http://www.cnblogs.com/yin52133/archive/2012/07/12/2588201.html#2611619

代碼可以從這裏下載:

http://download.csdn.net/detail/wojiao555555/6015003

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章