Question

I am developing a project where user records audio by click record button and stop the audio by clicking stop button, when user press stop button, popup is opening which ask to enter name, by this name audio will be stored in SDcard.

Now my problem is when user press stop button an error is occurring saying that java.lang.IllegalStateException.

Below is my activity code.

public class Record_Audio extends Activity {

private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4,
        MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4,
        AUDIO_RECORDER_FILE_EXT_3GP };
Chronometer myChronometer;
Handler seekHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.record_audio);

    myChronometer = (Chronometer) findViewById(R.id.chronometer);
    Button buttonStart = (Button) findViewById(R.id.btnStart);

    buttonStart.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(Record_Audio.this,
                    "Start Recording With LongClick", Toast.LENGTH_SHORT)
                    .show();
            enableButtons(true);
            startRecording();
            return true;
        }
    });

    setButtonHandlers();
    enableButtons(false);
    setFormatButtonCaption();
}

private void setButtonHandlers() {
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.allrecording)).setOnClickListener(btnClick);
}

private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}

private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    enableButton(R.id.btnFormat, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}

private void setFormatButtonCaption() {
    ((Button) findViewById(R.id.btnFormat))
            .setText(getString(R.string.audio_format) + " ("
                    + file_exts[currentFormat] + ")");
}

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);

    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + Code.audioName + file_exts[currentFormat]);
}

private void startRecording() {
    recorder = new MediaRecorder();

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    myChronometer.setBase(SystemClock.elapsedRealtime());
    myChronometer.start();
}

private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

        displayAlertDialog();

        recorder = null;
        myChronometer.stop();
    }
}

private void displayFormatDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String formats[] = { "MPEG 4", "3GPP" };

    builder.setTitle(getString(R.string.choose_format_title))
            .setSingleChoiceItems(formats, currentFormat,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            currentFormat = which;
                            setFormatButtonCaption();

                            dialog.dismiss();
                        }
                    }).show();
}

private void displayAlertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            Record_Audio.this);

    // Setting Dialog Title
    alertDialog.setTitle("Would you Like to save your Recording");

    // Setting Dialog Message
    alertDialog.setMessage("Enter Audio Name");

    final EditText editTextAudioName = new EditText(Record_Audio.this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    editTextAudioName.setLayoutParams(lp);
    alertDialog.setView(editTextAudioName);

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.save_icon);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Save",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    Code.audioName = editTextAudioName.getText().toString().trim();
                }
            });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    dialog.cancel();
                }
            });
    alertDialog.show();
}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record_Audio.this, "Error: " + what + ", " + extra,
                Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record_Audio.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnStart: {
            Toast.makeText(Record_Audio.this, "Start Recording",
                    Toast.LENGTH_SHORT).show();
            enableButtons(true);
            startRecording();
            break;
        }
        case R.id.btnStop: {
            Toast.makeText(Record_Audio.this, "Stop Recording",
                    Toast.LENGTH_SHORT).show();
            enableButtons(false);
            stopRecording();
            // displayFormatDialog();
            break;
        }
        case R.id.btnFormat: {
            displayFormatDialog();
            break;
        }

        case R.id.allrecording: {
            Intent intent = new Intent(Record_Audio.this, PlayAudio.class);
            startActivity(intent);
            break;
        }
        }
    }
};
}

And below is my logcat

E/AndroidRuntime(14328): FATAL EXCEPTION: main
E/AndroidRuntime(14328): java.lang.IllegalStateException
E/AndroidRuntime(14328):    at android.media.MediaRecorder.stop(Native Method)
E/AndroidRuntime(14328):    at iqualtech.skirr.Record_Audio.stopRecording(Record_Audio.java:113)
E/AndroidRuntime(14328):    at iqualtech.skirr.Record_Audio.access$2(Record_Audio.java:111)
E/AndroidRuntime(14328):    at iqualtech.skirr.Record_Audio$3.onClick(Record_Audio.java:221)
E/AndroidRuntime(14328):    at android.view.View.performClick(View.java:3517)
E/AndroidRuntime(14328):    at android.view.View$PerformClick.run(View.java:14155)
E/AndroidRuntime(14328):    at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(14328):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(14328):    at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime(14328):    at android.app.ActivityThread.main(ActivityThread.java:4624)
E/AndroidRuntime(14328):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(14328):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(14328):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
E/AndroidRuntime(14328):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
E/AndroidRuntime(14328):    at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

Take file name before starting the recording as it is coming NULL...
So open your pop up and on the click of pop up button, start the recording :

recorder = new MediaRecorder(); 
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(output_formats[currentFormat]); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile(getFilename()); 
recorder.setOnErrorListener(errorListener); 
recorder.setOnInfoListener(infoListener);

OTHER TIPS

Do like this,

private void startRecording() {
    displayAlertDialog();
}

and inside displayAlertDialog()

private void displayAlertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            Record_Audio.this);

    alertDialog.setTitle("Would you Like to save your Recording");
    alertDialog.setMessage("Enter Audio Name");
    alertDialog.setIcon(R.drawable.save_icon);

    final EditText editTextAudioName = new EditText(Record_Audio.this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    editTextAudioName.setLayoutParams(lp);
    alertDialog.setView(editTextAudioName);

    alertDialog.setPositiveButton("Save",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    Code.audioName = editTextAudioName.getText().toString()
                            .trim();

                    recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(output_formats[currentFormat]);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setOutputFile(getFilename());
                    recorder.setOnErrorListener(errorListener);
                    recorder.setOnInfoListener(infoListener);
                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    myChronometer.setBase(SystemClock.elapsedRealtime());
                    myChronometer.start();
                }
            });

    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    dialog.cancel();
                }
            });
    alertDialog.show();
}

IllegalStateException is thrown because when you call stop(), recorder is not in started state. refer Documentation:

Throws
IllegalStateException if it is called before start()

move all the lines which are required to start the player from stopRecording method to startRecording method:

private void startRecording() {
    recorder = new MediaRecorder();

//below lines have been added here from stopRecording
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);

The exception is coming because the steps mentioned in Documentation are not followed properly

--Create a new instance of android.media.MediaRecorder.
--Set the audio source using MediaRecorder.setAudioSource().
--Set output file format using MediaRecorder.setOutputFormat().
--Set output file name using MediaRecorder.setOutputFile().
--Set the audio encoder using MediaRecorder.setAudioEncoder().
--Call MediaRecorder.prepare() on the MediaRecorder instance.
--To start audio capture, call MediaRecorder.start().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top