Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Media Recording: java.lang.RuntimeException: start failed

I am working on records a phone calls. When i start a record a phone call then it's unfortunately stop. & it's gives error MediaRecorder start fail -2147483648. I refer this answer link. But i don't understand. Please tell me what is the problem in my code? Here is my Code.

public class IncomingCall extends BroadcastReceiver {

Context pcontext;
private static MediaRecorder recorder;
private boolean recordedStart = false;

@SuppressWarnings("unchecked")
public void onReceive(Context context, Intent intent) {
    pcontext = context;
    recorder = new MediaRecorder();

try {
            TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {



        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            Log.e("MyPhoneListener",state+"   incoming no:"+incomingNumber);
            Log.e("CALL_STATE_RINGING", "CALL_STATE_RINGING");

            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:

            recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            recorder.setOutputFile(Environment.getExternalStorageDirectory()+"/MyRecorder.mp3");
            Log.e("Path", ""+Environment.getExternalStorageDirectory()+"/MyRecorder.mp3");
            try {
                recorder.prepare();
                recorder.start();
                recordedStart = true;
                Log.e("Start", "Recorder Start");
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                Log.e("Error", ""+e);
            }

            break;
        case TelephonyManager.CALL_STATE_IDLE:
            Log.e("CALL_STATE_IDLE", "CALL_STATE_IDLE");
            if (recordedStart == true) {
                recorder.stop();
                recorder.release();
                recordedStart = false;
                Log.e("Stop", "Recorder Stop");
            }
            break;
        }
    }
}

}

Logcat Error

05-05 10:57:23.771: E/MediaRecorder(12812): start failed: -2147483648
05-05 10:57:23.771: D/AndroidRuntime(12812): Shutting down VM
05-05 10:57:23.771: W/dalvikvm(12812): threadid=1: thread exiting with uncaught exception (group=0x416bdd40)
05-05 10:57:23.774: E/AndroidRuntime(12812): FATAL EXCEPTION: main
05-05 10:57:23.774: E/AndroidRuntime(12812): Process: web.revolution.autocallanswer, PID: 12812
05-05 10:57:23.774: E/AndroidRuntime(12812): java.lang.RuntimeException: start failed.
05-05 10:57:23.774: E/AndroidRuntime(12812):    at android.media.MediaRecorder.start(Native Method)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at web.revolution.autocallanswer.IncomingCall$MyPhoneStateListener.onCallStateChanged(IncomingCall.java:98)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:389)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at android.os.Looper.loop(Looper.java:136)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at java.lang.reflect.Method.invoke(Method.java:515)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-05 10:57:23.774: E/AndroidRuntime(12812):    at dalvik.system.NativeStart.main(Native Method)
05-05 10:57:25.586: I/Process(12812): Sending signal. PID: 12812 SIG: 9
05-05 10:57:27.052: E/CALL_STATE_IDLE(13255): CALL_STATE_IDLE
like image 689
Jigar Shekh Avatar asked May 05 '14 06:05

Jigar Shekh


2 Answers

this problem causing by this Audio source

   recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

not working on your device and android! change that to

   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

or other sources.

also you can remove FC by change recorderstart() to try/catch with IllegalStateException,Exception same as this

        try {
        recorder.prepare();

    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        Log.d("ERROR ","IllegalStateException");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("ERROR ","IOException");
        e.printStackTrace();
    } 
    try {
    recorder.start();
    } catch (Exception e) {

    }

i had this problem my self,this is working great for call recording but having problem with receiver voice quality is very low if you find solution of this tel me too.

like image 150
Biftor Avatar answered Nov 02 '22 22:11

Biftor


I faced the same problem when I tried to record VOICE_CALL/VOICE_DOWNLINK/VOICE_UPLINK on my Nexus5. I don't want to talk long, this problem is indecisive on many smartphones under Android. But there are several devices that work's normally with this audio source. One of them is Samsung specifically Note3 (Android 4.3). I tried this on that device and everything been allright. As I heard that this problem will appear again when the update 4.4.2/3 reaches Note3, probably because of conflicts with U.S. law. But now you may use this feature on Note3 without any conditions.

If you use this audio source on other device you will have to prevent the crash of application with this code:

try {
    mRecorder.start();
    } catch (Throwable t) {
        t.printStackTrace();
        Log.w(LOG_TAG, t);
    }

Then you will not any problems with this issue...

What other device that would work fine with this source, I don't know. I tested only on Note3. Interesting how it's work on Galaxy3/4/5.

like image 29
user3725751 Avatar answered Nov 02 '22 20:11

user3725751