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???