如何在android中实现语音命令识别?

时间:2015-02-25 12:41:22

标签: android voice-recognition voice-recording

您好,我想构建一个应用程序,我的Android应用程序识别我的语音命令&执行某项任务。我搜索了很多,但没有找到任何有效的解决方案。请告诉我们如何实施它?

1 个答案:

答案 0 :(得分:3)

如果您想要离线语音识别,可以查看this question

但还有另一个解决方案:

您可以使用几年前谷歌制作的this app。 只需在您的设备中安装此应用,然后只需拨打该应用:

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;
    }
 }
}