record/save audio from voice recognition intent record/save audio from voice recognition intent android android

record/save audio from voice recognition intent


@Kaarel's answer is almost complete - the resulting audio is in intent.getData() and can be read using ContentResolver

Unfortunately, the AMR file that is returned is low quality - I wasn't able to find a way to get high quality recording. Any value I tried other than "audio/AMR" returned null in intent.getData().

If you find a way to get high quality recording - please comment or add an answer!

public void startSpeechRecognition() {   // Fire an intent to start the speech recognition activity.   Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);   // secret parameters that when added provide audio url in the result   intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");   intent.putExtra("android.speech.extra.GET_AUDIO", true);   startActivityForResult(intent, "<some code you choose>");}// handle result of speech recognition@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {    // the resulting text is in the getExtras:    Bundle bundle = data.getExtras();    ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS)    // the recording url is in getData:    Uri audioUri = data.getData();    ContentResolver contentResolver = getContentResolver();    InputStream filestream = contentResolver.openInputStream(audioUri);    // TODO: read audio file from inputstream}


Last time I checked, Google Keep set these extras:

  • android.speech.extra.GET_AUDIO_FORMAT: audio/AMR
  • android.speech.extra.GET_AUDIO: true

These are not documented as part of the Android documentation, so they do not constitute an Android API. Also, Google Keep does not rely on the recognizer intent to consider these extras. It would certainly be nice if such extras were popularized and documented by Google.

To find out which extras are set by Google Keep when it calls the RecognizerIntent, implement an app that responds to the RecognizerIntent and print out all the extras that it receives. You can also install Kõnele (http://kaljurand.github.io/K6nele/), which is an implementation of RecognizerIntent. When Kõnele is launched by Google Keep, then long-press the wrench-shaped settings icon. This shows some technical details about the caller, and includes also the incoming extras.

The answer by @Iftah explains how Google Keep returns the audio recording to the caller of RecognizerIntent.


I got this answer from here, I checked the dates and saw it was posted few days after your post, so I figured you missed it.Android speech recognizing and audio recording in the same time

one dude there says:

I got a solution that is working well to have speech recognizing and audio recording. Here (https://github.com/katchsvartanian/voiceRecognition ) is the link to a simple Android project I created to show the solution's working. Also, I put some print screens inside the project to illustrate the app.

I'm gonna try to explain briefly the approach I used. I combined two features in that project: Google Speech API and Flac recording.

Google Speech API is called through HTTP connections. Mike Pultz gives more details about the API:

"(...) the new [Google] API is a full-duplex streaming API. What this means, is that it actually uses two HTTP connections- one POST request to upload the content as a “live” chunked stream, and a second GET request to access the results, which makes much more sense for longer audio samples, or for streaming audio."

However, this API needs to receive a FLAC sound file to work properly. That makes us to go to the second part: Flac recording

I implemented Flac recording in that project through extracting and adapting some pieces of code and libraries from an open source app called AudioBoo. AudioBoo uses native code to record and play flac format.

Thus, it's possible to record a flac sound, send it to Google Speech API, get the text, and play the sound that was just recorded.

The project I created has the basic principles to make it work and can be improved for specific situations. In order to make it work in a different scenario, it's necessary to get a Google Speech API key, which is obtained by being part of Google Chromium-dev group. I left one key in that project just to show it's working, but I'll remove it eventually. If someone needs more information about it, let me know cause I'm not able to put more than 2 links in this post.