Matt Fritze
Matt Fritze

Reputation: 325

How to activate speech to text with a button?

I would like to implement a button that when clicked would activate android's speech to text translator like the one provided by android's keyboard. Specifically, I would like a button that would have the app transcribe what the user is saying in real time, and record it word by word (real time) in an editText box. What would be the best way to go about doing this?

Thanks

Upvotes: 8

Views: 8401

Answers (3)

M. Usman Khan
M. Usman Khan

Reputation: 4408

private void startVoiceRecognitionActivity()
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
    startActivityForResult(intent, REQUEST_CODE);
}

/**
 * Handle the results from the voice recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {
        // Populate the wordsList with the String values the recognition engine thought it heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        myEditText.setText(matches.get(0));
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Upvotes: 0

Jubba Smail
Jubba Smail

Reputation: 1227

In your app, you call startActivityForResult() using the ACTION_RECOGNIZE_SPEECH action. This starts the speech recognition activity, and you can then handle the result in onActivityResult().

private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        // Do something with spokenText
    }
    super.onActivityResult(requestCode, resultCode, data);
}

More info can be found in the reference

Upvotes: 0

Shobhit Puri
Shobhit Puri

Reputation: 26007

If you have not already checked the Voice Recognition sample in your Api demos, you should go ahead and check it. It should give you a head start. The demos are available in /android-sdk/samples/... folder. If you haven't installed them, here is how you may how to install android api demo app into my phone .

There are following( any many others) tutorial as well which will help you begin:

1) Android Voice Recognition Tutorial

2) Android: Speech To Text using API

Following might be a good read as well:

Add Text-To-Speech and Speech Recognition to Your Android Applications and Using the Android Speech Recognition APIs.

Hope this helps.

Upvotes: 2

Related Questions