{"id":681,"date":"2019-11-17T11:44:21","date_gmt":"2019-11-17T01:44:21","guid":{"rendered":"https:\/\/learntodroid.com\/?p=681"},"modified":"2019-11-17T11:44:29","modified_gmt":"2019-11-17T01:44:29","slug":"how-to-record-audio-in-android-programmatically","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-record-audio-in-android-programmatically\/","title":{"rendered":"How to Record Audio in Android Programmatically"},"content":{"rendered":"\n

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<\/a>, that listens for a song playing to discover the song name and artist, as well as Otter<\/a>, the app that listens and takes meeting notes from your verbal conversations.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

Android offers a MediaRecorder class<\/a> 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.<\/p>\n\n\n\n

Step<\/strong> <\/th>Step Description<\/strong> <\/th><\/tr><\/thead>
1<\/td>Request the permission to record audio in the manifest file <\/td><\/tr>
2<\/td>Request the permission to record audio when creating the Activity<\/td><\/tr>
3<\/td>Add UI components such as Buttons to trigger the voice recording<\/td><\/tr>
4<\/td>Use the MediaRecorder class to start a voice recording when the user selects a Button to start the recording<\/td><\/tr>
5<\/td>Use the MediaRecorder class to stop the voice recording when the user selects a Button to stop the recording<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

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.<\/p>\n\n\n\n

MediaRecorder Class Summary<\/h2>\n\n\n\n

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.<\/p>\n\n\n\n

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