In Android for recording audio or video, there is a built-in class called MediaRecorder. This class in Android helps to easily record video and audio files. The Android multimedia framework provides built-in support for capturing and encoding common audio and video formats. In android for recording audio, we will use a device microphone along with MediaRecorder Class and for recording video, we will use the user’s device Camera and MediaRecorder Class. Now in this article, we will see the implementation of an audio recorder in Android with an example.
Important Methods of MediaRecorder Class
|setAudioSource()||This method will specify the source of the audio to be recorded.|
|setAudioEncoder()||This method is used to specify the audio encoder.|
|setOutputFormat()||This method is used to specify the output format of our audio.|
|setOutputFile()||This method is used to specify the path of recorded audio files that are to be stored.|
|stop()||This method is used to stop the recording process.|
|start()||This method is used to start the recording process.|
|release()||This method is used to release the resource that is associated with the Media recorder class.|
Now we are creating a simple audio recorder app in which we will record audio from users device microphone and then we will store this audio recording in users device. We will also play this saved audio recording. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add permissions in the AndroidManifest.xml file
Add below line in the AndroidManifest.xml file.
Step 3: Modify the colors.xml and strings.xml file
Below is the code for the colors.xml file.
Below is the code for the strings.xml file.
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
Step 5: Working with the MainActivity.java file
Navigate to the app > java > Your app’s package name > MainActivity.java. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
All drawables are stored in the drawable folder. Navigate to the app > res > drawable folder to see all drawables. Now run the app on the Physical device to test it.
Output: Run on Physical Device
GitHub link for the project: https://github.com/ChaitanyaMunje/QR_Code_Scanner/tree/Audio_recorder