Vipin NU
Vipin NU

Reputation: 311

Voice Call Recording in Android 5.1

I have an application whcih records incoming and outgoing voice calls and it is working fine on devices with marshmallow or higher versions.But when i tried running it on android 5.1.1 it gives me error and app stops responding.

 The error shown in logcat is-ava.lang.RuntimeException: Unable to start service com.android.hitech.calls.Unused.MyRecordingService@2cb0b090 with Intent { cmp=com.android.hitech.calls/.Unused.MyRecordingService (has extras) }: java.lang.IllegalStateException
                                                    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2925)
                                                    at android.app.ActivityThread.access$2100(ActivityThread.java:151)
                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1408)
                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                    at android.os.Looper.loop(Looper.java:135)
                                                    at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
                                                 Caused by: java.lang.IllegalStateException
                                                    at android.media.MediaRecorder.start(Native Method)
                                                    at com.android.hitech.calls.Unused.MyRecordingService.onStartCommand(MyRecordingservice.java:88)
                                                    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2908)

My code for recording the voice calls is-

   recorder = new MediaRecorder();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        System.out.println("Present in MIC");
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    } else {
        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALLS);

   }
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    recorder.setAudioEncodingBitRate(16);
    recorder.setAudioSamplingRate(44100);
    recorder.setOnErrorListener(this);
    recorder.setOnInfoListener(this);
    try {
        recorder.prepare();
        recorder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

Edit- I did as suggested in the answer below but i am the error is still there.The call state is being called only once now by tweaking code a little bit but the app still crashes and even exception is the same.

Upvotes: 1

Views: 320

Answers (1)

Rushikant Pawar
Rushikant Pawar

Reputation: 458

Because in android 5.1 android system triggers CALL_STATE twice every time...!! i.e. once your NEW_OUTGOING_CALL is started or if any incoming call STATE_RINGING , STATE_ONHOOK and STATE_IDLE gets triggered twice..!! You have to manage it via code.


You can find reference what the problem is over 5.1 here... This is causing error

Upvotes: 1

Related Questions