Voice recognition on Android

Asked

Viewed 1,118 times

2

I did some research and need help to implement voice recognition in an application with Android Studio, I’m having some difficulties in finding support material. Thanks in advance!

1 answer

1


Basically you need to create an intention using the 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 run the code for example on any button. Just after the intent is completed after the end of the speech, will be called the onActivityResul. See 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 the intent is finalized, the onActivityResult shall recognise according to the value assigned to the variable. Example:

private final int REQ_CODE_SPEECH_INPUT = 7;
  • White, thanks for the help, but when trying to follow your orientation the following error happens falling on the catch line of your same code: "android.content.Activitynotfoundexception: No Activity found to Handle Intent { Act=android.speech.action.RECOGNIZE_SPEECH (has extras) }" I did equal your code, just putting the block Primiero inside the one button Onclick event... There’s something else I need to do, some more setup ?

  • @Eduardorafaelmoraes if giving Exception is because the device you are testing has no support for voice recognition. (If that’s what I got). The settings are exactly these for the basics. I created an app just to test and it worked perfectly. Anything, detail more your problem here q we try to solve together.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.