Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on MediaRecorder Stop : stop called in invalid state 4

Tags:

java

android

I'm creating a recording voice app, when I'm trying to stop recording the Debug Console in java says that: "MediaRecorder stop called in an invalid state : 4" Here is part of my code:

import java.io.File;
import java.io.IOException;
import com.androidexample.tabbar.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Tab1 extends Activity implements OnClickListener{
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 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    Button btnStart =(Button)findViewById(R.id.btnStart);
    btnStart.setOnClickListener(this);

    Button btnStop =(Button)findViewById(R.id.btnStop);
    btnStop.setOnClickListener(this);

    Button btnFormat =(Button)findViewById(R.id.btnFormat);
    btnFormat.setOnClickListener(this);


    enableButtons(false);
    setFormatButtonCaption();
}

private void setButtonHandlers() {

}

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();
    String filepath =Environment.getExternalStorageDirectory() + File.separator 
            + Environment.DIRECTORY_DCIM + File.separator + "FILE_NAME";
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);


    if (!file.exists()) {
        file.mkdirs();
    }

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}

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

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    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();
    }
}

private void stopRecording() {
    if (null != recorder){

            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;

    }
}

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 MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Tab1.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(Tab1.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnStart : 
    Toast.makeText(Tab1.this, "Start Recording",
            Toast.LENGTH_SHORT).show();

    enableButtons(true);
    startRecording();

    break;
case R.id.btnStop : 
    Toast.makeText(Tab1.this, "Stop Recording",
            Toast.LENGTH_SHORT).show();
    enableButtons(false);
    stopRecording();

    break;

case R.id.btnFormat : 
    displayFormatDialog();

    break;



}

}

}

And this is my Log Cat Information :

07-30 15:11:21.972: E/MediaRecorder(836): stop called in an invalid state: 4
07-30 15:11:21.972: D/AndroidRuntime(836): Shutting down VM
07-30 15:11:21.972: W/dalvikvm(836): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 15:11:21.982: E/AndroidRuntime(836): FATAL EXCEPTION: main
07-30 15:11:21.982: E/AndroidRuntime(836): java.lang.IllegalStateException
07-30 15:11:21.982: E/AndroidRuntime(836):  at android.media.MediaRecorder.stop(Native Method)
like image 688
AminH86 Avatar asked Oct 31 '22 19:10

AminH86


1 Answers

private void stopRecording() {
    if (null != recorder){
        try {
            recorder.prepare();  <-- Here's the problem

You should not be calling prepare when you're stopping the MediaRecorder. The prepare method is something you call prior to starting the recorder. Refer to the state diagram in the MediaRecorder documentation.

like image 59
Michael Avatar answered Nov 08 '22 06:11

Michael