Why do I get IllegalStateException on stopping recording audio?
Asked Answered
S

1

6

I want to record audio with the microphone and save the audio file. Start recording works fine, but when I try to stop recording, the emulator gives a force close error. Stack trace:

01-09 18:16:59.075: E/AndroidRuntime(831): FATAL EXCEPTION: main
01-09 18:16:59.075: E/AndroidRuntime(831): java.lang.IllegalStateException
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.media.MediaRecorder.stop(Native Method)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.example.voice.recorder.MainActivity.StopRecording(MainActivity.java:45)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.example.voice.recorder.MainActivity$1.onClick(MainActivity.java:76)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.view.View.performClick(View.java:3511)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.view.View$PerformClick.run(View.java:14105)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.os.Handler.handleCallback(Handler.java:605)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.os.Looper.loop(Looper.java:137)
01-09 18:16:59.075: E/AndroidRuntime(831):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-09 18:16:59.075: E/AndroidRuntime(831):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:16:59.075: E/AndroidRuntime(831):  at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-09 18:16:59.075: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-09 18:16:59.075: E/AndroidRuntime(831):  at dalvik.system.NativeStart.main(Native Method)

It gives an error on MediaRecorder.stop(); This is how I try to stop recording:

public void StopRecording() throws IOException{
    recorder.stop();
    recorder.reset();
    recorder.release();
    recorder = null;
}

How I start recording:

public class MainActivity extends Activity {
    MediaRecorder recorder;
public void StartRecording(){
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile("/sdcard/sample.3gp");
    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}   

And how I call the method:

            if (!tv.getText().equals("Recording...")){
                tv.setText("Recording...");
                tv.setTextColor(Color.RED);
                record.setImageResource(R.drawable.microphone_icon_pressed);
                StartRecording();

            }else{
                tv.setText("Click the button to start recording");
                record.setImageResource(R.drawable.microphone_icon);
                tv.setTextColor(Color.BLACK);
                try {
                    StopRecording();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

I have this 2 permissions in my manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

So start recording works fine, but stop recording doesn't. Does anybody know what's wrong with the code?

Suttles answered 9/1, 2013 at 18:28 Comment(5)
Please do not repeat the tag in the question. I've edited it out.Suttles
Could you add a bit more context for your code, e.g. how/where you're declaring recorder? (if it's a non-static member of a BroadcastReceiver you could run into these sorts of problems, for example).Welker
Did you fulfil steps 3, 4, 5, 6 from audio capture guide?Deste
I edited my question with code of how I start recordingSuttles
I didn't mean just how you start the recorder, but also how and where you declare it. What type of object are these variables and methods part of?Welker
B
1

Your recorder obviously not in recording state. you should make sure if it is started succesfully. Because IllegalStateException occurs when calling stop() before start(). And add RuntimeException in your stop() block if it is thrown then delete the output file.

See MediaRecorder.java

  /**
     * Stops recording. Call this after start(). Once recording is stopped,
     * you will have to configure it again as if it has just been constructed.
     * Note that a RuntimeException is intentionally thrown to the
     * application, if no valid audio/video data has been received when stop()
     * is called. This happens if stop() is called immediately after
     * start(). The failure lets the application take action accordingly to
     * clean up the output file (delete the output file, for instance), since
     * the output file is not properly constructed when this happens.
     *
     * @throws IllegalStateException if it is called before start()
     */
    public native void stop() throws IllegalStateException;

I also recommend not to release recorder object until your app is closed if you need to start and stop consecutively. According to the following flow, create recorder onCreate()/onResume() and release onPause/onDestroy().

enter image description here

Bertrando answered 7/4, 2016 at 21:10 Comment(1)
But the OP's code includes a call to the start() method, so how does this answer help to solve the issue?Smoker

© 2022 - 2024 — McMap. All rights reserved.