Android get the Intent when implementing RecognitionListener

JavaAndroidAndroid IntentAndroid Bundle

Java Problem Overview


As stated in this answer by Iftah I can get the audio recorded by the Speech Recoginition in android by getting the Uri from the Intent passed to:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // the recording url is in getData:
    Uri audioUri = data.getData();
}

Here the Intent data has exactly what I want without problems.

That all works perfectly, however that solution puts up a prompt for the user of when to speak, I did not want that so to get around that I let my activity implement RecognitionListener as so:

public class MainActivity extends AppCompatActivity implements RecognitionListener {

    private SpeechRecognizer speech = null;
    private Intent recognizerIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "sv_SE");
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "sv_SE");
        recognizerIntent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
        recognizerIntent.putExtra("android.speech.extra.GET_AUDIO", true);

        speech = SpeechRecognizer.createSpeechRecognizer(this);
        speech.setRecognitionListener(this);
        speech.startListening(recognizerIntent);
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d("Debug", "On ready for speech");
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.d("Debug", "On beggining of speech");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.d("Debug", "Rsm changed");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d("Debug", "Buffer recieved");
    }

    @Override
    public void onEndOfSpeech() {
        Log.d("Debug", "On end of speech");
    }

    @Override
    public void onError(int error) {
        Log.d("Debug", "Error");
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d("Debug", "On partial result");
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d("Debug", "On event");
    }

    @Override
    public void onResults(Bundle results) {
        Log.d("Debug", "On result");
    }
}

This gets around the prompt hovewer I can not figure out how to get the Uri like in the first example because here:

@Override
    public void onResults(Bundle results) {
        Log.d("Debug", "On result");
        // The results bundle don't contain the URI!
    }

I get a Bundle results which don't contain the Intent or the Uri. I have tried looking at all the keys in the bundle and no URI or Intent exists, I have also tried getIntent() but that don't return anything.

I appreciate any help or push in the right direction.

Java Solutions


Solution 1 - Java

The results should be in the bundle with the key SpeechRecognizer.RESULTS_RECOGNITION.

@Override
public void onResults(Bundle results) {
    if (results != null && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION)) {
      List<String> resultsList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      // most likely result is resultsList.get(0)
    } else {
      // no results
    }
    
}

See the Android developer documentation for SpeechRecognizer explains it in detail. This method for getting speech recognition results also works for partial results in #onPartialResults

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFredrikView Question on Stackoverflow
Solution 1 - JavaajankenmView Answer on Stackoverflow