開始做這個的時候,從網上當了一段代碼,但後來測試老是提示沒有找到設備。。。非常煩人。。。
經過多方查找資料,發現需要裝一個Google語音的插件,運行語音識別的時候要用到。如果沒有就提示沒有找到設備。
下載地址:http://download.csdn.net/detail/wojiao555555/6014985
代碼如下:
- public class RecognizerIntentActivity extends Activity {
- private Button btnReconizer;
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.reconizer);
- btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);
- btnReconizer.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- try{
- //通過Intent傳遞語音識別的模式,開啓語音
- 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);
- }catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "找不到語音設備", 1).show();
- }
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- //回調獲取從谷歌得到的數據
- if(requestCode==VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){
- //取得語音的字符
- ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- String resultString="";
- for(int i=0;i<results.size();i++){
- resultString+=results.get(i);
- }
- Toast.makeText(this, resultString, 1).show();
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
上邊的那個需要連網,因爲是把語音信號發送到google服務器上進行比對、識別的。所以這次打算弄個離線的demo,就是開着航班模式也能識別的。demo。
參考如下:
http://www.cnblogs.com/yin52133/archive/2012/07/12/2588201.html#2611619
代碼可以從這裏下載: