Android 语音识别

来源:互联网 发布:低风险网络基金产品 编辑:程序博客网 时间:2024/06/05 19:14

1,判断是否支持语音识别

imagebtn = (ImageButton)findViewById(R.id.imageBtn01);       imagebtn.setOnClickListener(new View.OnClickListener() {                                       @Override           public void onClick(View v) {               // TODO Auto-generated method stub     try{ 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,REQUEST_CODE);         }catch(ActivityNotFoundException e){   e.printStackTrace();    Toast.makeText(MianActivity.this , "找不到语音设备",     Toast.LENGTH_SHORT).show();                                  }                                       }       });

2,启动语音识别

ArrayList<String> results= data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);           //listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list)); //把数据显示在listview中           String resultsString = "";           resultsString =resultsString+results.get(0);

3,获取识别结果,并以Toast显示出来

Toast.makeText(MianActivity.this, resultsString, Toast.LENGTH_SHORT).show();           super.onActivityResult(requestCode, resultCode,data);


本文出自 “卡布奇诺” 博客,请务必保留此出处http://tianxiaoyong.blog.51cto.com/6962715/1228145

原创粉丝点击