I want to make a voice recorder app but it crashes when i click the \"Start Recording\" button. I get an error saying java.lang.IllegalStateException at android.media.Media
you are forgetting to call recorder.prepare()
before recordeer.start()
function in your beginRecording
function.
Prepare function will take care about lot of things like conversion of analog data to digital audio for compresion and where to store the file etc
`if (audioRecorder != null) {
try {
audioRecorder.stop();
audioRecorder.reset();
audioRecorder.release();
audioRecorder = null;
} catch (IllegalStateException e) {
e.printStackTrace();
}
}`
Because we are not releasing recorder and only stopping the recorder on relaunch we get at android.media.MediaRecorder.start(Native Method) took 1 hrs to figure out
Call this after setOutFormat() but before prepare().
this is just what my android studio docs dialog says while i write this method name.
the point is that you should call this method just before prepare().
here is an example:
private void startRecording() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MediaMaster/Dub/");
Log.i(TAG, "startRecording: creating output file " + outputFolder.mkdirs());
File output = new File(outputFolder.getAbsolutePath()+"out" + new Date().getTime() + ".3gpp");
mediaRecorder.setOutputFile(output.getAbsolutePath());
mediaRecorder.setMaxDuration(3000);
try {
mediaRecorder.prepare();
} catch (IOException e) {
Log.e(TAG, "startRecording: ", e);
}
mediaRecorder.start();
}
You have to take into consideration, that MediaRecorder as well as MediaPlayer has their state machines, which obligate you to do some action in specific sequence.
Here you tried to start recording withou preparing MediaRecorder
. Call
recorder.prepare();
Before:
recorder.start();
Caused by: java.lang.IllegalStateException at android.media.MediaRecorder.native_start(Native Method)
I was facing issue regarding media recorder. It was recording perfect but one i try to record the screen again then i get this error and media recorder gets crashed. After trying many things and tatics i tried this mediarecorder.release(); with this method every time i stop the recording the media recorder gets released and enables me to record again.