Voice recognition on Android [closed]

2

I did some research and I need help implementing speech recognition in an application with Android Studio, I'm having some difficulties finding support material. Thank you!

    
asked by anonymous 20.08.2017 / 22:50

1 answer

1

You basically need to create an intent using RecognizerIntent . See the explanation in the code:

// cria um intent usando para abrir a tela de captura de voz
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// configuração para caputarar fala baseado no local padrão do dispositivo
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
// configuração definir titulo no alert 
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Fala alguma coisa agora");
try {
    // faz a chamadada do ActivityResult com o código de resgate
    startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
    // mostra uma mensagem caso o dispositivo não possua suporte
    Toast.makeText(getApplicationContext(), "Não há suporte", Toast.LENGTH_SHORT).show();
}

You can create a function and call execute the code for example on any button. Soon after intent is finalized after the end of speech, onActivityResul will be called. Here's how it should look:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {
                // aqui recebe a fala do usuário depois intent desaparecer
                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                // esse txtSpeachInput é um TextView que você pode criar
                // para receber a voz do usuário usando result.get(0)
                txtSpeechInput.setText(result.get(0));
            }
            break;
        }
    }
}

The REQ_CODE_SPEECH_INPUT can be created with any value. It only ensures that when intent is finalized, onActivityResult will recognize according to the value assigned to variable. Example:

private final int REQ_CODE_SPEECH_INPUT = 7;
    
21.08.2017 / 00:42