androidbroadcastreceiverillegalstateexceptionphone-state-listener

IllegalStateException in MediaRecorder.start()


I saw this question asked many times. But I didn't find a solution.

I have a broadcast receiver in which I record calls. recorder.start() is throwing IllegalStateException. I gave all permissions in Manifest. I called recorder.prepare() (It doesn't throw any exception.)

Did I miss anything?

Here is my simplified code:

case TelephonyManager.CALL_STATE_OFFHOOK:
   recordState = true;

                  MediaRecorder mRecorder = new MediaRecorder();
                        mRecorder
                                .setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                        mRecorder
                                .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mRecorder
                                .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecorder.setOutputFile(filePath);
                        mRecorder.setOnErrorListener(new OnErrorListener() {

                            @Override
                            public void onError(MediaRecorder mr, int what,
                                    int extra) {

                            }
                        });
                        mRecorder.setOnInfoListener(new OnInfoListener() {

                            @Override
                            public void onInfo(MediaRecorder mr, int what,
                                    int extra) {
                                // TODO Auto-generated method stub

                            }
                        });
                        mRecorder.prepare();
                        mRecorder.start();
                        recorder = mRecorder;

case TelephonyManager.CALL_STATE_IDLE:
if (recorder != null && recordState) {
                            recorder.stop();
                            recorder.reset();
                            recorder.release();
                            recorder = null;

                        }

Here is my logcat:

07-21 14:23:08.807: E/MediaRecorder(4202): start failed: -38
07-21 14:23:08.807: W/System.err(4202): java.lang.IllegalStateException
07-21 14:23:08.807: W/System.err(4202):     at android.media.MediaRecorder.start(Native Method)
07-21 14:23:08.807: W/System.err(4202):     at com.example.recordtest.MyReceiver$MyPhoneStateListener.onCallStateChanged(MyReceiver.java:165)
07-21 14:23:08.807: W/System.err(4202):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
07-21 14:23:08.807: W/System.err(4202):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 14:23:08.807: W/System.err(4202):     at android.os.Looper.loop(Looper.java:137)
07-21 14:23:08.807: W/System.err(4202):     at android.app.ActivityThread.main(ActivityThread.java:4856)
07-21 14:23:08.807: W/System.err(4202):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 14:23:08.807: W/System.err(4202):     at java.lang.reflect.Method.invoke(Method.java:511)
07-21 14:23:08.817: W/System.err(4202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-21 14:23:08.817: W/System.err(4202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-21 14:23:08.817: W/System.err(4202):     at dalvik.system.NativeStart.main(Native Method)

Edit An empty file is created at the given path.

EDIT:

Now I created a dummy app with two buttons, one to start and the other to stop recording. Here is the code and throws the same exception on calling start():

package com.example.recordtest;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    MediaRecorder mRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = (Button) findViewById(R.id.button1);
        Button btn2 = (Button) findViewById(R.id.button2);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                File file = new File(Environment.getExternalStorageDirectory(),
                        "1234.3gp");
                mRecorder.setOutputFile(file.getAbsolutePath());
                mRecorder.setOnErrorListener(new OnErrorListener() {

                    @Override
                    public void onError(MediaRecorder mr, int what, int extra) {

                    }
                });
                mRecorder.setOnInfoListener(new OnInfoListener() {

                    @Override
                    public void onInfo(MediaRecorder mr, int what, int extra) {
                        // TODO Auto-generated method stub

                    }
                });
                try {
                    mRecorder.prepare();
                    mRecorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mRecorder.stop();
                mRecorder.reset();
                mRecorder.release();
            }
        });

    }

}

Solution

  • After scratching my head a couple of hours, I realized. It was a device issue. The above code just works fine in the other device.

    I knew that Call Recording can't be done in all the devices(Manufacturers restrict it). But never knew it throws an IllegalStateException.