簡單透過 Intent 開啟內建的語音辨識 Activity ...
private Button btnDialog;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDialog = (Button) this.findViewById(R.id.btn1);
textView = (TextView) this.findViewById(R.id.tv1);
btnDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//透過 Intent 的方式開啟內建的語音辨識 Activity...
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "請說話..."); //語音辨識 Dialog 上要顯示的提示文字
startActivityForResult(intent, 1);
}
});
}
接著 override onActivityResult() 處理回傳的辨識文字。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
//把所有辨識的可能結果印出來看一看,第一筆是最 match 的。
ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String all = "";
for (String r : result) {
all = all + r + "\n";
}
textView.setText(all);
}
}
}
繼續閱讀: Android 語音辨識 App 開發 (二) - 不開語音辨識 dialog 的背景辨識方法
沒有留言:
張貼留言