How to record voice call using AudioSource.VOICE_CALL
Asked Answered
S

5

5

trying to record call, I am using MediaRecorder class when using AudioSource.MIC or AudioSource.VOICE_COMMUNICATION its recording only my voice not from recevier and when I use AudioSource.VOICE_CALL it gives exception on attending call.. here is code

if(intent.getAction().equals("android.intent.action.PHONE_STATE")){
            if((bundle = intent.getExtras()) != null){
                state = bundle.getString(TelephonyManager.EXTRA_STATE);
                if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                    inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    wasRinging = true;
                    Toast.makeText(context, inCall + " is calling", Toast.LENGTH_SHORT).show();
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    if(wasRinging){
                        Toast.makeText(context, "Call Answered", Toast.LENGTH_SHORT).show();
                        Date date = new Date();
                        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");

                        String filename = "rec_" + format.format(date) + ".mp3";
                        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath();
                        String fileUri = path + "/" + filename;
                        Log.v("testing uri", fileUri);
                        File file = new File(fileUri);

                        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                        recorder.setOutputFile(file.getAbsolutePath());

                        try {
                            recorder.prepare();
                            recorder.start();
                            recording = true;

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                    wasRinging = false;
                    Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show();
                    if(recording && recorder != null){
                        recorder.stop();
                        recorder = null;
                        recording = false;
                    }
                }
            }
        }

And this is error..

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } in com.asadullah.callrecorder.MyBroadCastReceiver@41d6c7a8
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5102)
   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:785)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: start failed.
   at android.media.MediaRecorder.start(Native Method)
   at com.asadullah.callrecorder.MyBroadCastReceiver.onReceive(MyBroadCastReceiver.java:62)
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)

manifests permissions are:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.STORAGE" />
Simonasimonds answered 14/7, 2017 at 8:19 Comment(0)
P
5

VOICE_CALL is deprecated now. That is why this error occurred.

Use VOICE_COMMUNICATION as AudioSource as it is microphone audio source tuned for voice communications such as VoIP.

I am also working with call recording app but it failed in Android 7.1.1

If you are not trying call record on Android 7.1.1 below code will work.

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Portwine answered 16/2, 2018 at 13:8 Comment(4)
will your suggested code record mic as well as speaker voice too? means all two way voices?Simonasimonds
yes my suggestion is worked for me besides 7.1.1, try this one this will be worked for you. if you cannot hear voice from receiver using VOICE_COMMUNICATION try MediaRecorder.AudioSource.DEFAULT as well.Portwine
@SudarshanaDayananda hi i tried all the way bro, still not able to get the result for recording. can you help me to solve my issue?Chloroform
I have tried recording call using VOICE_COMMUNICATION but it only records from mic not the sound coming from speaker. Any suggestion?Langur
T
2

In order to use VOICE_CALL you need to take special permission.

 <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"
   tools:ignore="ProtectedPermissions" />
Test answered 8/6, 2018 at 11:22 Comment(1)
How do you take this special permission? Is it really possible?Noisy
G
1

I think you require some permissions before recording in newer version of android (Api 23). Check out this SO question - Recording calls in android why this not works

Permissions

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

And if you require more help you can even check official document for more info regarding this error.

Gliadin answered 14/7, 2017 at 8:43 Comment(0)
T
1

I think you forgot to take this permission.

    <uses-permission android:name="android.permission.CALL_PHONE" />
Test answered 8/6, 2018 at 11:13 Comment(1)
How can you get this permission exactly?Noisy
C
0

You need the CAPTURE_AUDIO_OUTPUT permission for Voice_Call, which is documented here.

But you will NOT get the permission for the reason stated here.

Note the line "Not for use by third-party applications."

Casuistry answered 2/11, 2017 at 11:15 Comment(10)
So how could it help to add it, if you can't have it granted?Noisy
If your app is not a third party app.Casuistry
Is it possible, using root, to have this permission?Noisy
I believe so, however, I a not sure how.Casuistry
I guess you add your app as a system app and then do the request as normal for capture_audio_output and voice_call which you should then be granted.Casuistry
I've tried it, but it doesn't seem to really grant it. Doesn't even return that it was granted.Noisy
I am no expert on this at all, however it is is something like; root device, generate certificate for the app, add the certificate into the folder for system layer certificates, sign the app with the certificate and then install - it should then be get the permissions.Casuistry
I don't know how to do it using root. Can you please show in code?Noisy
I am sorry nor do I, I have not done it before and the above is a theoretical approach. You could try androidauthority.com/…Casuistry
Is it just on my web browser, or does it have weird lines of code there, of "CPP" , "exit" , as well as bullets ?Noisy

© 2022 - 2024 — McMap. All rights reserved.