k.nadonenko
k.nadonenko

Reputation: 628

Android IllegalStateException when recording audio on some devices

I want my program to record audio for 10 seconds and then stop and store my record in file storage, everything works fine on my Nexus 4 and Galaxy S 5, but when i test it in Galaxy S3 it crushes and raise error

10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/AudioCaptureDemo﹕ prepare() failed 10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/MediaRecorder﹕ start called in an invalid state: 4 10-02 02:13:44.942 1279-1279/com.taptester.tappapp D/AndroidRuntime﹕ Shutting down VM 10-02 02:13:44.942 1279-1279/com.taptester.tappapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a12ba8) 10-02 02:13:44.952 1279-1279/com.taptester.tappapp E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.taptester.tappapp, PID: 1279 java.lang.IllegalStateException at android.media.MediaRecorder.start(Native Method) at com.taptester.tappapp.MainActivity.startRecording(MainActivity.java:203) at com.taptester.tappapp.MainActivity.access$300(MainActivity.java:62) at com.taptester.tappapp.MainActivity$6.onFinish(MainActivity.java:825) at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

First i thought the error is in the file name, I'm declaring it like this:

public MainActivity() {
    mFileName = Environment.getExternalStorageDirectory() + File.separator
            + Environment.DIRECTORY_DCIM + File.separator + "MyMemo.3gp";
    //Environment.getExternalStorageDirectory().getAbsolutePath();
    //mFileName += "/MyMemo.3gp";
}

Then i make a record like this:

private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

    try {
        mRecorder.prepare();
        mRecorder.start();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }


}

Then i call the "StartRecording" method like this:

else if(command.equals("2")) {

                    startRecording();

                    Toast.makeText(getApplicationContext(), "Start recording...",
                            Toast.LENGTH_SHORT).show();

                    CountDownTimer start = new CountDownTimer(timer, 1000) {

                        @Override
                        public void onTick(long l) {
                            Toast.makeText(getApplicationContext(), "Recording!!!",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onFinish() {
                            stopRecording();

                        }
                    }.start();

Upvotes: 0

Views: 1391

Answers (1)

Bharat Kumar Molleti
Bharat Kumar Molleti

Reputation: 141

  1. you are not supposed to call mRecorder.start(); when mRecorder.prepare(); fails.

this is what caused the Illegal State Exception to be thrown.

  1. Not all devices support all the encoding formats. try changing these mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); and mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

change the format and Encoder to a file format and encoding format your s3 device supports.

for a start, change your encoding setting to default settings as follows and try if it works. mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

Upvotes: 1

Related Questions