main.xml
<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:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onText" android:text="語音辨識" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onWeb" android:text="網頁搜尋" /> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>SpeechRecognizerActivity
public class SpeechRecognizerActivity extends Activity { private static final String TAG = "SpeechRecognizerActivity"; private TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.tv = (TextView) this.findViewById(R.id.tv); } public void onText(View v) { Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); it.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說..."); this.startActivityForResult(it, 1); } public void onWeb(View v) { Intent it = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); it.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說..."); // 不用等結果 this.startActivity(it); } @Override public void onActivityResult(int requestCode, int resultCode, Intent it) { if (requestCode != 1) { Log.d(TAG, "不是語音辨識"); return; } if (resultCode != RESULT_OK) { Log.e(TAG, "語音辨識失敗"); return; } List<String> list = it.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); StringBuilder text = new StringBuilder(); for (String s : list) { text.append(s + "\n"); } this.tv.setText(text); } }必須開啟網路,否則會出現以下的錯誤。
成功了,語音辨識。
語音辨識查詢網路。
請問這個你這個程式,須要先安裝Google Voice嗎?
回覆刪除請問你這個程式,須要先安裝Google Voice嗎
回覆刪除請問這個你這個程式,須要先安裝Google Voice嗎?
回覆刪除請問你這個程式,須要先安裝Google
回覆刪除Voice嗎?
我當初是沒另外安裝,但不知道現在狀況是怎麼。
回覆刪除