首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

语音识别——练习题中

2012-08-17 
语音识别——练习中模拟器看不出效果,手机上可以正常语音方法:public class VoiceActivity extends Activity

语音识别——练习中

模拟器看不出效果,手机上可以正常语音

方法:

public class VoiceActivity extends Activity {private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;private ListView mlist;private String resultString;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mlist = (ListView) findViewById(R.id.lists);Button button = (Button) findViewById(R.id.btn);button.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {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,VOICE_RECOGNITION_REQUEST_CODE);} catch (Exception e) {Toast.makeText(VoiceActivity.this, "找不到语音设备装配",Toast.LENGTH_SHORT).show();e.printStackTrace();}}});}// 当语音结束时的回调函数onActivityResult@Overrideprotected 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);// 设置试图更新// mlist.setAdapter(new ArrayAdapter<String>(this,,// android.R.layout.simple_list_item_1,results));String resultsString = "";for (int i = 0; i < results.size(); i++) {resultString += results.get(i);}Toast.makeText(this, resultsString, Toast.LENGTH_SHORT).show();}super.onActivityResult(requestCode, resultCode, data);}}

?

?

在main.xml中布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btn"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="开始语音识别" />    <ListView        android:id="@+id/lists"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </ListView></LinearLayout>

?

热点排行