How to Record Audio in Android Programmatically


There are some great apps on the Android Play Store that use voice recording functionality from your mobile device, some great examples include apps such as Shazam, that listens for a song playing to discover the song name and artist, as well as Otter, the app that listens and takes meeting notes from your verbal conversations.

I have done some research and created very basic voice recorder app in Android and published the source code to GitHub. I will share with you in this article how to record audio programmatically in Android.

Android offers a MediaRecorder class to record audio from the built-in microphone on your Android device. In order to make use of the MediaRecorder class to record audio programmatically in Android, follow the steps below.

Step Step Description
1 Request the permission to record audio in the manifest file
2 Request the permission to record audio when creating the Activity
3 Add UI components such as Buttons to trigger the voice recording
4 Use the MediaRecorder class to start a voice recording when the user selects a Button to start the recording
5 Use the MediaRecorder class to stop the voice recording when the user selects a Button to stop the recording

In the next section, I will go into detail on the functionality offered by the MediaRecorder class. From there I will take you through the step by step process for recording audio in Android.

MediaRecorder Class Summary

Before you can utilise the MediaRecorder to record audio, you will need to ensure you have obtained the permission to record audio via both the app’s manifest file and the main activity. I will cover this in the tutorial below.

Assuming you have permission to record audio, in order to start recording, you will first need to import the MediaRecorder class then create a new instance of the MediaRecorder class.

From there you will need to set up your audio recorder by setting the audio source, audio encoder, output format and output file on your MediaRecorder instance.

To start recording use the prepare method and start method on the MediaRecorder.

Please see a summary of the important methods from the MediaRecorder Class.

Method Description
setAudioSource(int) This method sets the audio source to be used for recording. The audio source needs to be set before setting the output format.

For the microphone audio source use MediaRecorder.AudioSource.MIC

Other examples for audio sources can be found in the AudioSource documentation
setOutputFormat(int) This method sets the format of the output file for the audio recording.

For the 3GPP format use MediaRecorder.OutputFormat.THREE_GPP

Other examples for output formats that you can use are available in the OutputFormat documentation.

Please note the MediaRecorder can be used for recording both video and audio.
setAudioEncoder(int) The method sets the audio encoder to be used for the audio recording.

For the AMR (narrowband) audio codec use MediaRecorder.AudioEncoder.AMR_NB

Other examples for audio encoders that you can use are available in the AudioEncoder documentation.
setOutputFile(String) This method sets the path of where the output file will be generated for the audio recording.
prepare() This method prepares the recorder to being capturing and encoding data
start() This method starts capturing and encoding the data from the recorder to the file specified in the setOutputFormat(int) method
stop() This method stops recording. In order to start recording again, you will need to reconfigure the recorder and call prepare then start.
release() This method will release the resources associated with the MediaRecorder. Generally is a good practice to call release after stopping the recording with the stop method.
pause() The method will pause the recording and will allow you to resume using the resume() method
resume() The method will allow you to resume recording if your recording has been paused using the pause() method

Requesting Permission to Record Audio in Android

In order to record audio within your app you will need to use the android.permission.RECORD_AUDIO permission.

Due to the potentially privacy risks to the user when recording audio, the Android team have classified the RECORD_AUDIO permission as a dangerous permission.

Like all permissions you will need to request the RECORD_AUDIO permission within your Android app’s manifest file.

In addition to this, because the RECORD_AUDIO permission is a dangerous permission you will need to get the users explicit consent to use this permission.

This can be done through a run time prompt requesting permission to use RECORD_AUDIO within your app. I will show you how to do this in an Activity in the step by step tutorial below.

Steps to Record Audio Programmatically in Android

The next section of this post will include a step by step tutorial to show you how to record audio programmatically in Android.

This example code is available in a public GitHub repository called AudioRecorder.

Requesting Permission to Record Audio in the Manifest

In order to record audio using the MediaRecorder class, you will need to include the RECORD_AUDIO permission in your app’s manifest file.

Your manifest file can be located at the path app/src/main/AndroidManifest.xml

Inside the manifest tag above the application tag, add a “uses-permission” element requesting the “android.permission.RECORD_AUDIO” permission.

Requesting Permission to Record Audio in the Activity

Due to the privacy risks, the Android team consider the RECORD_AUDIO permission is considered a dangerous permission.

With dangerous permissions, you will need to obtain explicit consent for the user to use those permissions. If you don’t do this a SecurityException will be thrown if the app attempts to utilise a dangerous permission which has not been granted consent.

To get the explicit consent from the user to use the RECORD_AUDIO permission, do the following inside the MainActivity class.

  • Define a String array of the permissions you will be asking for consent to use. In this case we are only interested in Manifest.permission.RECORD_AUDIO
  • Define a boolean to check whether the audio recording permission has been created, defaulting it to false
  • Define a constant integer representing the request for obtaining the permission to record audio
  • Override the onRequestPermissionsResult method to check that the permission to record audio has been granted and update the boolean value accordingly. If the permission has not be granted close the app using the finish() method
  • Inside the onCreate(…) method call the ActivityCompat.requestPermissions(…) method to request the audio recording permission

Adding UI Components to the Voice Recorder App

I will trigger the starting and stopping of the audio recording via Buttons in the user interface.

For the layout of the MainActivity, I will create a basic vertical oriented LinearLayout and display a Button to start the audio recording and a button to stop the audio recording.

Below is an excerpt from the activity_main.xml layout file.

Inside the MainActivity class in the onCreate(…) method I will call setContentView(R.layout.activity_main); to utilise the layout file

Using the MediaRecorder Class to Start and Stop Recording of the Audio

In order to start recording audio using the MediaRecorder perform the following steps:

  1. Create a new instance of the MediaRecorder class
  2. Set the Audio Source on the MediaRecorder to MediaRecorder.AudioSource.MIC
  3. Set the Output Format on the MediaRecorder to MediaRecorder.OutputFormat.THREE_GPP
  4. Set the Output File path on the MediaRecorder
  5. Set the Audio Encoder on the MediaRecorder to MediaRecorder.AudioEncoder.AMR_NB
  6. Call the prepare() method on the MediaRecorder
  7. Call the start() method on the MediaRecorder

To stop recording audio perform the following steps:

  1. Call the release() method on the MediaRecorder
  2. Call the stop() method on the MediaRecorder

Below is an excerpt from the MainActivity.java class that shows how to start and stop recording of the audio

How to play an audio recording in Android

Android offers a MediaPlayer class which supports the ability for your to play audio and video files within your app.

For a list of all of the formats supported by the MediaPlayer check out the Supported media formats documentation.

I have enhanced the sample app created above for recording audio programmatically in Android to also include the ability to playback your audio recording once you have stopped recording. It also allows you to stop playing the audio while it is being played.

In order to play an audio recording using MediaPlayer you need to follow these simple steps.

  1. Create a new instance of the MediaPlayer
  2. Set the data source of the player to the file you want to play back using the setDataSource(String) method
  3. Use the setOnCompletionListener(…) method if you want to perform certain functionality once the audio file has completed playback
  4. Call the prepare() method on the player
  5. Call the start() method on the player

Below is an excerpt from the activity_main.xml layout file which has got some additional Buttons to be used for playing back the audio recording.

Below is an excerpt from the the MainActivity.java class that shows how to playback and stop the playback of an audio recording.

In addition to the sample code shared in this blog post, a public repository for this project has been created called AudioRecorder containing all of the code covered above.

Recent Posts