The android platform has a very easy way that we could capture audio; either from the gadgets microphone, or a call. Lets see how we will build a custom recording application.
The Android multimedia framework includes support for capturing and encoding a variety of common audio formats, so that you can easily integrate audio into your applications. You can record audio using the MediaRecorder APIs if supported by the device hardware and playback audio using MediaPlayer. This write-up will introduce you on how to record audio and play it back.
Note: The Android Emulator does not have the ability to capture audio, but actual devices are likely to provide these capabilities.
You will need the following tools to accomplish the purpose of this write-up:
Basically we are starting off with an empty activity template. What this means is that we are given the whole authority to choose which ever way we want our launcher (first activity that shows) to be.
Designing the view
Enough of the talks. Lets do some action. We will start off first with the view of the application. We need to put our designing swag on as this will need a little bit of artistic mind to finish it up. Okay, I came up with the following design.
Making the app work
Now lets outline the steps that the app will go through to call it a working application.
Launch the app.
Click the START RECORD button and record your sound.
Eventually click the STOP RECORD button to stop recording.
Click the PLAY RECORD button to play the sound you recorded.
Eventually click the STOP PLAYBK to stop the playback.
Click the REFRESH REC to start the process over again.
Simple right? Lets take this little by little. First lets see how our MainActivity.java file should look like before we start editing it.
Now lets create an instance of all the elements on the view and every other variables we might need.
Now lets go on and write some code for the one function/method that controls all our major actions or events.
To record a sound you have to do the following:
set the source of the recording to the phone’s microphone
make output format .3gpp
encode the recorded file
set the output file
prepare and start recording
This is how the startRecord() function should look like:
This is how the stopRecord() function should look like:
This is how the playRecord() should look like:
This is how the stopPlayback() should look like:
This is how the refreshActivity() should look like:
Now finally, to make the application not hang when recording sound we need to seek permission from the device to use it’s microphone and also gain access to write a file to the external storage. All these should be done in the AndroidManifest.xml file. This is how this file should look like:
After you are sure you understand the processes taken to record a media file and play it back, test the application on a physical device - not an emulator, and leave your issues and feedback in the comments box. Whoops this is lengthy, but it is worthwhile.