简体   繁体   English

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

[英]How to implement voice command recognition in android?

Hello I want build an app where my android app recognize my voice command & perform certain task. 您好,我想构建一个我的Android应用能够识别我的语音命令并执行某些任务的应用。 i have searched a lot but didn't find any valid solution. 我已经搜索了很多,但没有找到任何有效的解决方案。 Kindly tell how to implement it? 请告诉我如何执行它?

you may take a look at this question if you want offline speech recognition: 如果您要离线语音识别,可以查看以下问题

But there is also a another solution: 但是还有另一个解决方案:

You can use this app which is made by google a few years ago. 您可以使用几年前由Google制造的此应用 just install this app in your device and then simply call that 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();
    }
}

Then in onActivityResult() do this : 然后在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;
    }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM