我想使用 RecognizerIntent 和默认的 android wear UI 来实现关键词语音识别。
问题是我无法更改 android wear watch 监听的默认语言。由于识别错误,我只想识别英文单词,我不想更改手机的默认语言。我正在做这样的事情:
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
mTextView.setText(spokenText);
if (spokenText.contains("KeyWord")) {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] vibrationPattern = {0, 500, 50, 300};
//-1 - don't repeat
final int indexInPatternToRepeat = -1;
vibrator.vibrate(vibrationPattern, indexInPatternToRepeat);
}
// Do something with spokenText
}
super.onActivityResult(requestCode, resultCode, data);
}
该解决方案有效,但也没有给我英文结果。语音识别由android wear watch 完成。