The Android multimedia framework includes support for recording and playing audio. In this blog post, we’ll develop a basic Sound Recorder application that is capable of recording audio and saving it into the local storage of an Android device using the MediaRecorder that is provided by the Android SDK.
You will also learn how to request user-permissions in real-time and how to work with the local storage of an Android device.
Creating the UI
First, we need to build the user interface of the Audio Recorder. It consists of a simple layout with 3 buttons which will be used to start, pause, resume and stop the recording.
Requesting the required permissions
After creating the UI we can almost start using the MediaRecorder to build our app. But first, we need to request the required permissions to record the audio and access the local storage. We can do that with some simple lines of code in our AndroidManifest.xml file.
We also need to check if the user has really enabled the permissions before we can use our MediaRecorder. We can do so in our MainActivity.kt file.
Note: This lines of code will later be moved in the OnClickListener call of the start_recording button so we can make sure that the MediaRecorder will not be started without having the right permissions.
Recording and saving audio
First, we need to add OnClickListeners to the buttons to make sure they react to user events. As I mentioned before the check for the right permissions will be added in the OnClickListener call of the start_recording button.
Configuring the MediaRecorder
Next, we need to define a path for our output and start configuring our MediaRecorder.
Here, we get the path to the root of our external storage and add our recording name and filetype to it. After that, we create our MediaRecorder object and define the audio source, audio encoder, output format and output file.
Recording and saving the audio
The code used to start the MediaRecorder is defined in the OnClickListener of the start_recording button.
As you can see we need to call the prepare function before we can start the recording. We also embed it into a try-catch block to make sure the app won’t crash when the prepares function fails.
The OnClickListeners of the stop button is very similar to the one we defined above.
Here we check if the MediaRecorder is currently running before we actually stop the recording because our app would crash if the stop method is called while the MediaRecorder isn’t recording. After that, we change the state variable to false to prevent the user from pressing the stop button again.
After that, we just need to define the OnClickListener for the pause/resume button.
In these two methods we check if the MediaRecorder is running. If so we pause the recording and change the text of the button to resume. If clicked again the recording will resume from the point it left of.
Finally, we can start recording audio and listen to it by opening the recording.mp3 file that will be saved in our local storage.
Complete Source Code for the MainActivity.kt
Here you can get the complete source code for our basic Sound Recorder app:
Looking up the audio file
After recording you need to go into the local storage of your Android device to listen to your recordings. I’m just including it for the people who aren’t confident navigating in the local Android storage.
- Open the files app on your Android device
- Go to the local register
- Search for recording.mp3
Now you should know how the MediaRecorder works, how you can request permission in real-time and why it is important to do so. You have also learned about the local storage of your Android device and how you store data in it.
A more complex version of this app which has some extra features like playing your recordings using the MediaPlayer is also available on my Github.
If you have found this useful, please consider recommending and sharing it with other fellow developers.
If you have any questions or critics, you can reach me in the comment section.
Originally published at gabrieltanner.org.