Android Audio Recording Tutorial
03/11/2009
After many hours of trying to record audio on my Google Android device, I’ve finally arrived at a workable solution. There were a few bumps along the way besides the horribly out of date MediaRecorder documentation, which was sorely lacking details. For one, I could only get audio to record to the SD card. Additionally, the directory being recorded to must already exist before attempting to record to it. Without further ado, here is a complete example for recording audio on the Android via the MediaRecorder API:
package com.benmccann.android.hello;
import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;
/**
* @author <a href="http://www.benmccann.com">Ben McCann</a>
*/
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
Guys,
I’m looking for a working apk that will live stream audio from my android microphone input to a URL or IP address of my choice via wireless or 3G connection. Any recommendation or referral would be appreciated. This is a great forum, unfortunately I have very little knowledge of programming. Thanks and more power to you all!
This following piece of code is not working . Does it require anything else also to set the frame rate of video captured.
mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
Thank you a bunch for sharing this with all folks you really recognise what you are talking about! Bookmarked. Kindly also consult with my site =). We can have a link change arrangement between us
Thanx a lot 🙂 !! worked for me ..i was wandering for such code but other codes available had bugs
Thank you 🙂 its working great
Hi,
Actually i want to replace my new recording replaces the old recording in android voice recorder . please tel some ideas. thank you
for saving the voice file on sd card ,your code alone is ok,or i want to do any changes for your code.
How automatic call recording and call answering can be created in android apps???
Dude I want to process live audio – check for particular things in it, like whistle sounds
Got a working example?
Is it possible to record DOWNLINK and UPLINK voice into two different channels or files?
When the voice of both parties saved in one channel (both caller and other party merged together), since the voice of the MIC is much louder it would be very hard to hear any voice of the other side of the call.
What is the solution?
Yes dear it is partially possible. You have to recognize sound first. Then you can save them separately. After that you can amplify other party sound. These links will help you. Best of luck. 🙂
http://stackoverflow.com/questions/8524182/sound-recognition-in-android
http://stackoverflow.com/questions/14181449/android-detect-sound-level
Thanks Ben McCann.
How do I run this code that you’ve written here sir? can it run on desktop computer?
Hi,I want to develop a application like automatic call recorder.Could you tell me the flow and concepts used in that app.
Thanks in advancee…..
Works fine! Don’t forget to include next 3 lines in AndroidManifest.xml :
hey Ben!
how about the main activity ? what should i put there ?
Hi ,sir,
Could you detail step by step to build this app recording Audio, by save different files ?
I am the beginner android . so I wanna to know and can build app from you tutorial .
In this tutorial not see interface , so I would like you detail and step by step to build this app.
Thanks Advance ,
I am looking forward to your reply soon
hello Ben,
thank you for your valuable tutorial but after running this i got ‘stop called in an invalid state : 1’ and ‘IllegalStateException’ on new AudioRecorder(“/QuickRecord/recording”).stop(); call. please help me out. Thank you or anyone wants to help then most welcome.
Hello Ben,
I want to record audio in mp3 format not 3gp. How can I achieve it? Please help.
Thanks …!!!
Amazing!!
It was Helpful……..
Can you please help me just getting the amplitude from voice nearby without recording it in android?
This is not recording the incoming voice.Plz help to record call from both side clearly
MediaRecorder does not record both side audio recordings in android
I have posted on code –
https://stackoverflow.com/questions/52903735/mediarecorder-does-not-record-both-side-audio-recordings-in-android