如何在android中实现语音命令识别?
How to implement voice command recognition in android?
你好,我想构建一个应用程序,我的 android 应用程序可以识别我的语音命令并执行特定任务。我搜索了很多但没有找到任何有效的解决方案。请问如何实现?
如果你想要离线语音识别,你可以看看this question:
不过还有一个解决办法:
你可以用this app几年前google做的。
只需在您的设备中安装此应用,然后调用该应用即可:
private void startRecognizeSpeech() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast.makeText(
getApplicationContext(),
"Oops! First you must download \"Voice Search\" App from Store",
Toast.LENGTH_SHORT).show();
}
}
然后在 onActivityResult() 中执行此操作:
@Override
// calls when voice recognition result received
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
// text is received form google
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//you can find your result in text Arraylist
}
break;
}
}
}
你好,我想构建一个应用程序,我的 android 应用程序可以识别我的语音命令并执行特定任务。我搜索了很多但没有找到任何有效的解决方案。请问如何实现?
如果你想要离线语音识别,你可以看看this question:
不过还有一个解决办法:
你可以用this app几年前google做的。 只需在您的设备中安装此应用,然后调用该应用即可:
private void startRecognizeSpeech() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
} catch (ActivityNotFoundException a) {
Toast.makeText(
getApplicationContext(),
"Oops! First you must download \"Voice Search\" App from Store",
Toast.LENGTH_SHORT).show();
}
}
然后在 onActivityResult() 中执行此操作:
@Override
// calls when voice recognition result received
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
// text is received form google
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//you can find your result in text Arraylist
}
break;
}
}
}