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();
}
}
package com.AudioRecording;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudioRecording extends Activity
{
public static String path=null;
public static MediaRecorder recoder=null;
public static MediaPlayer player=null;
Button start=null;
Button stop=null;
Button Play=null;
public void setPath(String modPath)
{
AudioRecording.path=modPath;
}
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
this.setPath(“/sdcard/myoutputfile.3gp”);
start=(Button)findViewById(R.id.btnRecord);
Play=(Button)findViewById(R.id.startPlayerBtn);
Play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player=new MediaPlayer();
try {
player.setDataSource(“/sdcard/myoutputfile.3gp”);
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println(“Testing File\n”);
recoder=new MediaRecorder();
recoder.setAudioSource(MediaRecorder.AudioSource.MIC);
recoder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recoder.setOutputFile(AudioRecording.path);
try
{
recoder.prepare();
recoder.start();
}catch(IOException e)
{}
}
});
stop=(Button)findViewById(R.id.stopPlayerBtn);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
stopPlayingRecording();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void stopPlayingRecording() throws Exception {
if(recoder!=null)
{
recoder.stop();
}
}
}
This is the code i have written for recording audio through micro phone. I am able to record voice but i am unable to play back it, please help me to play
steps i have taken to execute this code is:
i created sdcard img and i stored the output_file int. But it is not playing
This shiit not work… Fuckin erorrs and crashed
its working
Hi all
I want running live video streaming on android. When run .flv file only sound hear but does not see video.
What problem not understand.
Please help me what problem you think.
Thanks
awinash
@dragonksn,
Check whether you have registered the activity in manifest.xml file.I think its the issue there.
Thanks for the tutorial, however i have a problem which i don’t know how to troubleshoot
Below is my code that uses the AudioRecorder class. I don’t have any problem compiling, but whenever i try to run using eclipse AVD, and presses the start recording button, it produces the error “The application has stopped unexpectedly. Please try again”, is it something wrong with my code or do i have to configure the AVD?
Please help!!
MY CODE:
package com.fyp;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FYP extends Activity {
private Button mStart;
private Button mStop;
private Button mPlay;
final AudioRecorder recorder = new AudioRecorder(“/audiometer/temp”);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mStart = (Button) findViewById(R.id.startButton);
mStop = (Button) findViewById(R.id.stopButton);
mPlay = (Button) findViewById(R.id.playButton);
mStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
recorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
mStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
recorder.stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
mPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
}
});
}
}
This code is not working ……..
m trying to run it on android emulator running android 1.5
its giving force close error……..
please help
Just take a look at class com.android.soundrecorder.Recorder.
hi! anyone can pls post working code on video capturing in motiuon on android, i m able to save in sd card but not able to capture in motion.
pls reply.
Hi Ben,
Thanks for posting this. It was very helpful.
I am trying to replicate the same recording behavior as the ‘Tom Cat’ app, where you speak into the phone and it plays back whatever you just said.
So it seems to:
– detect when you speak to start recording
– detect when you stop speaking to stop recording
– plays back exactly what you said, nothing more
I can start and stop recording with buttons but want to avoid button clicks. Not sure how they do it. Is it always recording and just tails what was spoken?
Can you point me in the right direction? Any help would be greatly appreciated. Thanks!
Wayne
Hi wayne
you can acheive this by checking the amplitude value. if the amplitude goes above certain level start recording and vice versa for stop recording. I haven’t done it myself however it should work
Saqib
Hi,
how to show the audio clip when recording complete. In my device, the clip of audio is not show in sdcard but after reboot the device then see in sdcard.
THANKS!
Hey does anyone know how to implement background video recording on android using services?
Hi,
Please let me know that how to record voice call in android .
Thanks.
How do i implement background video recording on android using services with the following codes?
package net.learn2develop.CameraTest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class CameraTest extends Activity implements SurfaceHolder.Callback {
private static final String TAG =”CAMERA_TUTORIAL”;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
private boolean previewRunning;
//File tempFile = null;
Button btnStart=null;
Button btnStop=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btnStart = (Button) findViewById(R.id.button4);
btnStart.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startRecording();
}
});
Button btnStop = (Button) findViewById(R.id.button5);
btnStop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopRecording();
}
});
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
if (camera != null) {
Camera.Parameters params = camera.getParameters();
camera.setParameters(params);
}
else {
Toast.makeText(getApplicationContext(), “Camera not available!”, Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (previewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(320, 240);
p.setPreviewFormat(PixelFormat.JPEG);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
previewRunning = true;
}
catch (IOException e) {
Log.e(TAG,e.getMessage());
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
camera.stopPreview();
previewRunning = false;
camera.release();
}
private MediaRecorder mediaRecorder;
private final int maxDurationInMs = 20000;
private final int videoFramesPerSecond = 20;
public boolean startRecording(){
try {
Toast.makeText(getBaseContext(), “Recording Started”, Toast.LENGTH_SHORT).show();
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setMaxDuration(maxDurationInMs);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
DateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH_mm_ss”);
Date date = new Date();
File directory = new File(Environment.getExternalStorageDirectory() + “/VideoList”);
if(!(directory.exists()))
directory.mkdir();
File FileSaved = new File(Environment.getExternalStorageDirectory() + “/VideoList”, dateFormat.format(date) + “.3gp”);
mediaRecorder.setOutputFile(FileSaved.getPath());
mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
//mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.prepare();
mediaRecorder.start();
return true;
} catch (IllegalStateException e) {
Log.e(TAG,e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
Log.e(TAG,e.getMessage());
e.printStackTrace();
return false;
}
}
public void stopRecording(){
Toast.makeText(getBaseContext(), “Recording Stopped”, Toast.LENGTH_SHORT).show();
mediaRecorder.stop();
camera.lock();
}
}
I have a problem says that “The Application VoiceRecorder (process com.training.VoiceRecorder) has stop unexpextedly. Please Try again”
and my code is the following
————For VoiceRecorder.java—————————
package com.training.VoiceRecorder;
import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;
public class VoiceRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public VoiceRecorder(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();
}
}
———————-For MyActivity.java———————
package com.training.VoiceRecorder;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final VoiceRecorder recorder = new VoiceRecorder(“/audiometer/temp”);
try {
recorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
recorder.stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I really dont know what’s the problem
Please help me cuz i really need to complete this app as quickly as possible.
Is it the wrong path file or I have to add any XML for main.xml
Please please please help me find the way out
“The application has stopped unexpectedly. Please try again”
Exactly the same error. Single Activity empty project.
Manifest contains both permissions, activity in manifest is registered, this is not my first app I wrote on Android.
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
path is String path = “/sdcard”;
It crashes on —-> recorder.prepare();
I have no damn idea 🙁
^^ I’ve been searching all night and haven’t been able to figure this out. recorder.prepare() throws IOException every time.. “prepare failed.”
when I do outgoing call.It is giving error………..
start failed..or sometime RunTime exception.
Plz let me Knw What Is The issue……..?
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class AudioRecordTest extends Activity
{
private String TAG = “AudioRecordTest”;
private static String FileName = null;
private MediaRecorder Recorder = null;
public static String callerName, date,path,number;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
Log.d(TAG, “I am in Oncreate”);
DBAdapter db = new DBAdapter(this);
db.open(SQLiteDatabase.NO_LOCALIZED_COLLATORS);
db.getPath();
path= db.audioPath;
db.close();
final TelephonyManager telephony = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener()
{
public void onCallStateChanged(final int state, final String incomingNumber)
{
try{
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
if(Recorder!=null)
{
Recorder.stop();
Recorder.release();
}
Log.d(“TestActivity”, “Call is idle.”);
finish();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(“TestActivity”, “Call connected”);
startRecording();
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
default:
break;
}
}
finally{
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
private void startRecording() throws RuntimeException
{
DBAdapter db = new DBAdapter(this);
if(isSdPresent())
{
System.out.println(“sd present”);
FileName = path+”/” + callerName + “_” +date + “.3gp”;
}
else
{
System.out.println(“sd card not present”);
FileName = “data/data/com.callplus”+”/” + callerName + “_” +date + “.3gp”;
}
db.open(SQLiteDatabase.NO_LOCALIZED_COLLATORS);
db.insertPathDetail(FileName);
db.close();
Log.e(TAG, “Filename is”+FileName);
db.open(SQLiteDatabase.NO_LOCALIZED_COLLATORS);
db.setLog(callerName,number,””,date);
db.close();
Recorder = new MediaRecorder();
Recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
Recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
Recorder.setOutputFile(FileName);
Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try
{
Recorder.prepare();
Recorder.start();
}
catch (IOException e)
{
Log.e(TAG, “Recording failed1111”);
}
catch (RuntimeException e)
{
e.printStackTrace();
Log.e(TAG, “Recording failed2222”);
}
}
}
Hi,
I am new to this group. I am trying to build Record and Play example in this link ” http://developer.android.com/guide/topics/media/index.html ” but while click on the recording button it said sorry “Application has stopped Unexpectedly” my code is here please help.
/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application’s AndroidManifest.xml file, with something like:
*
*
*
*
*/
package com.android.audiorecordtest;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = “AudioRecordTest”;
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, “prepare() failed”);
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, “prepare() failed”);
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText(“Stop recording”);
} else {
setText(“Start recording”);
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText(“Start recording”);
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText(“Stop playing”);
} else {
setText(“Start playing”);
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText(“Start playing”);
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += “/audiorecordtest.3gp”;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
Thanks and Regards,
C.Sujith
Hi guys, I need to combine two audio files stored in sdcard, and play as a single file. Could anyone suggest a code snippnet
Hi, I am new to Android programming. I want to make a recorder on Android. Can the emulator take the input from a microphone on Windows 7. If yes, is there any configuration I need to do or it will directly take the input from the microphone.
Hello Benj..
Can we able to record voice call in android..if so what are the packages that we need to import ..
i’m getting prepare failed exemption. 🙁
Seriously, this is awesome stuff. You simplified my life so much. This is what OOP is all about guys! Thanks again!
hi, i just want to ask how to record a voice in database of android? because we are making text to speech using syllable algorithm in our language..thanks in advance..
Hi Ben, et al,
I’m also trying to implement your code/class into an Android project, but I am using Processing, so I expect some of the package declarations etc are not the same as using a straight Java/Eclipse setup.
I’ve sat your class within my project, and am attempting to call the start() and stop() methods, but I’m not seeming to actually do so… If instead I call AudioRecorder.start(), i get a “non-static method start() cannot be referenced from a static context” error… I think this means that I am instantiating the object of the class properly, but I thought that your class already does that ?! (ie. ‘final MediaRecorder recorder = new MediaRecorder();’ …).. if I try to instantiate the class in setup(), (ie. ‘ record = new AudioRecord(this);’ … i get a ‘cannot find symbol’ error… )
I would really like to get this going, but am a bit of an OOP newbie, so if anyone can kindly point out any obvious errors I am making, I would be most humbled and thankful.
I can also post full code (and attempt to explain some of the Processing idiosyncrasies for you pure Java heads…) as needed…
thx
Jesse
Hey Jesse,
You need to instantiate AudioRecorder before you call it’s start method since that method isn’t static.
AudioRecorder recorder = new AudioRecorder(…);
recorder.start();
hey guys,
im really new to android so sorry if i sound like a total idiot,
but is this for recording internal sounds within an android?
and where do you use this code & how?
cheers amazing people,
x
Nas, this is for recording sounds from the microphone on your Android device:
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Those who try on Emulator should know that, voice recording via emulator is not supported. You’ll need real dev device to test this code.
BTW anyone know how to pause the recording?? since recorder.pause() is not available, something like dat??
Hi Ben and others,
Thanks for the code! I also got the unsupported parameter error, but my audio clip is still recorded and it plays fine 😀
However I realized that if I want to record another clip, the app crashes without catching any exceptions… does anybody know why? Logcat shows a bunch of numbers and I have no idea what they mean:
INFO/DEBUG(1285): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
INFO/DEBUG(1285): Build fingerprint: ‘LGE/thunderg/thunderg/thunderg:2.2.1/FRG83/eng.nikech.choi.20110126.134422:user/release-keys’
INFO/DEBUG(1285): >>> com.bcit.chairlogger <<<
INFO/DEBUG(1285): signal 11 (SIGSEGV), fault addr 00000010
INFO/DEBUG(1285): r0 00000000 r1 00000000 r2 a930cc98 r3 00000001
……
INFO/DEBUG(1285): #00 pc 00033c28 /system/lib/libmedia.so
INFO/DEBUG(1285): #01 pc 0000780e /system/lib/libmedia_jni.so
……
INFO/DEBUG(1285): code around pc:
INFO/DEBUG(1285): a9033c08 2001e001 1c1861a0 46c0bd70 00029a58
……
INFO/DEBUG(1285): code around lr:
INFO/DEBUG(1285): a93077f0 f7ffb510 bd10ffcf b082b570 ae011c05
……
INFO/DEBUG(1285): stack:
INFO/DEBUG(1285): bef054d0 00000001
……
Thanks a bunch!
Oh to add to my previous post… I already declared permissions in manifest and I am testing on the device rather than the emulator.
Hi, me again.
Turns out the problem is with where the new object of the MediaRecorder is created. Since the object is released every time the stop method is called, if a new method is created in the start method instead, then things would work fine.
Thanks for the code 🙂
Is there anyway to connect two mono external mic’s to record on the Droid in stereo? I know the mic mini on the Droid is mono but what about having a Y “merger” go into the USB port?
Hi Ben, Hi guys
i have this problem, i use your example:
http://pastebin.com/GtnybAwP
http://pastebin.com/Z2PcAqz3
and the android manifest
http://pastebin.com/eTQFY9he
now when i click start button the app work, but when i click stop button i have the crack of app
can you help me ?
Best Regads
Antonio
hey pls someone post the correct code for recording a voice and store it….
thanks in advance:-)
Try to explain these things to ‘The Common People’.
Wouldn’t an AVI or MP4 container with XviD or H264 or DivX or … + MP3-sound have been easier to work with?
At least sth easily cutable, joinable, extractable?!
hi people.spotted this page on my quest to discover if i can somehow record audio into my google android via the headphone out….i know with a *cough* iphone you can if u use say one of these http://www.petersontuners.com/index.cfm?category=135&action=itemView&itemID=39 to achieve such as idea.im a dj and want to record audio in direct to the sd card as a wav with say tape machine app.be made up if u can help make this poss with ur knowledge 😀
hi people. i am building an android app to play all audio of formats (mp3 and wma) can any one help me with the code. i dont know java to code it. help me with the resource that is available to build it.
Hi Ben,
This tutorial is really helpfull for all Android Developers, keep it up buddy.
Hi guys,
Your post is really usefull for me. But i had another requirement.
I need to record a audio which is playing in media player of the same phone.
my intention is to add an record option for an internet radio.
what recordSource should i set for recording the streaming audio.
Can any one help me.
Thanks in advance.
Anoop Gopalakrishnan
Hi everyone,
I want to record video in .m4e file format anyone help me for this stuff .
Hi, I am looking for someone to develop an Android call recording solution. If you have done this *successfully* using a direct API model before and are interested, please contact me.
sorry but it’s my first time to use android code i want to know how to work with it and where to but this codes do i need another software than emulator ?
Hi, i am looking to record audio in an Android Phone from a Microphone conected to Headphone Jack. I have an LG ANdroid Phone ver 2.3.3. i bought a headphones with microphone integrated but it does not work, it supposed i believed that when you connect the Microphone the sound captured from Microphone will be hearing in the Phone Speakers, what microphone do you recommend to use i Android Phones?. or do i have to configure someting in the Phone? I appreciate your attention
Please send me an email to : eacosta1976@gmail.com
Hi. Does anyone knows where does the .3gp file saved at? If I passed in the file directory as /audiometer/temp, where can i find it on the emulator?
SIR can we detect which application of my phone is using my phone mic?
Magnificent web site. Plenty of helpful info here. I’m sending it to some friends ans additionally sharing in delicious. And obviously, thanks to your sweat!