Search code examples
androidvoice-recognitionvoice-recording

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. i have searched a lot but didn't find any valid solution. Kindly tell how to implement it?


Solution

  • 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. 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 :

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