Following is my code for recording a audio in my application
public class AudioRecordActivity 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;
static int i=1;
private void onRecord(boolean start) {
if (start) {
i++;
String name="AudioRecord"+i;
startRecording(name);
} 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(String fileName) {
File storageDir = new File(Environment
.getExternalStorageDirectory(), "/audio/");
storageDir.mkdir();
mRecorder = new MediaRecorder();
ContentValues values = new ContentValues(3);
values.put(MediaStore.MediaColumns.TITLE, fileName);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mRecorder.setOutputFile("/sdcard/sound/" + fileName);
try {
mRecorder.prepare();
} catch (Exception e){
e.printStackTrace();
}
final ProgressDialog mProgressDialog = new ProgressDialog(AudioRecordActivity.this);
mProgressDialog.setTitle("Recording");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mProgressDialog.dismiss();
mRecorder.stop();
mRecorder.release();
}
});
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface p1) {
mRecorder.stop();
mRecorder.release();
}
});
mRecorder.start();
mProgressDialog.show();
}
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 void 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;
}
}
}
The error which I am getting is as below:
03-12 16:58:22.847: E/AndroidRuntime(459): FATAL EXCEPTION: main
03-12 16:58:22.847: E/AndroidRuntime(459): java.lang.IllegalStateException
03-12 16:58:22.847: E/AndroidRuntime(459): at android.media.MediaRecorder.start(Native Method)
03-12 16:58:22.847: E/AndroidRuntime(459): at org.audio.AudioRecordActivity.startRecording(AudioRecordActivity.java:105)
03-12 16:58:22.847: E/AndroidRuntime(459): at org.audio.AudioRecordActivity.onRecord(AudioRecordActivity.java:39)
03-12 16:58:22.847: E/AndroidRuntime(459): at org.audio.AudioRecordActivity.access$0(AudioRecordActivity.java:35)
03-12 16:58:22.847: E/AndroidRuntime(459): at org.audio.AudioRecordActivity$RecordButton$1.onClick(AudioRecordActivity.java:120)
03-12 16:58:22.847: E/AndroidRuntime(459): at android.view.View.performClick(View.java:2485)
03-12 16:58:22.847: E/AndroidRuntime(459): at android.view.View$PerformClick.run(View.java:9080)
03-12 16:58:22.847: E/AndroidRuntime(459): at android.os.Handler.handleCallback(Handler.java:587)
03-12 16:58:22.847: E/AndroidRuntime(459): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 16:58:22.847: E/AndroidRuntime(459): at android.os.Looper.loop(Looper.java:123)
03-12 16:58:22.847: E/AndroidRuntime(459): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-12 16:58:22.847: E/AndroidRuntime(459): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 16:58:22.847: E/AndroidRuntime(459): at java.lang.reflect.Method.invoke(Method.java:507)
03-12 16:58:22.847: E/AndroidRuntime(459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-12 16:58:22.847: E/AndroidRuntime(459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 16:58:22.847: E/AndroidRuntime(459): at dalvik.system.NativeStart.main(Native Method)
Please look into the code. I am not getting the problem. Thanks in advance.
Take a closer look at the LogCat below:
03-12 16:58:22.847: E/AndroidRuntime(459): java.lang.IllegalStateException
03-12 16:58:22.847: E/AndroidRuntime(459): at android.media.MediaRecorder.start(Native Method)
From above logcat, we realize
start() threw an IllegalStateException. Consider MediaRecorder requires the following to be registered (in order):
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare(); // not properly processed
recorder.start(); // IllegalStateException occurred
Therefore,
We know prepare() was either (1) not called, or (2) called and had problems.
Place start() right below prepare() as follows:
try {
mRecorder.prepare();
mRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
Check for new warnings and problems you get from LogCat. Possible warnings/problems are:
(i) Problem caused by previous sets (e.g. incompatible/invalid output format, permission for AudioSource, etc).
(ii) Exceptions caused by previous sets (e.g. FileNotFoundException, etc).
Confirm prepare() resolves clearly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With