3

I have a problem in recording a call I have made a service and called a BroadcastReceiver to get the call state. In TelephonyManager.EXTRA_STATE_OFFHOOK when the call is received. I am using following code to record the call

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);                               recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
     recorder.prepare();
     recorder.start();
} 
catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) { 
e.printStackTrace();
} catch (Exception ex)
{
ex.printStackTrace();
}

This code is working fine and creates the audio file but when I listen the audio file I can only listen my outgoing voice, caller voice is not recorded.

When I use

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

instead of

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

in above code it throws exception of recoder fails on recoder.start();

So, how can I record voice call?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
user2064024
  • 531
  • 2
  • 7
  • 20

4 Answers4

4

I also had the same doubt a year ago AudioSource.VOICE_CALL not working in android 4.0 but working in android 2.3

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); works on limited devices ,it will give exception only on that device in which voice call is not supported so catch the exception and start the recording from mic all over again that will keep you on safer side in non supported device.

Community
  • 1
  • 1
Auto-Droid ツ
  • 1,583
  • 1
  • 17
  • 31
  • so what should i have to do in this case ? – user2064024 Jun 10 '14 at 11:23
  • when i use .MIC it doesnot records incoming voice it only records my voice – user2064024 Jun 10 '14 at 11:28
  • which device you are using – Auto-Droid ツ Jun 10 '14 at 11:32
  • 1
    audioSource = MediaRecorder.AudioSource.CAMCORDER; try this – Auto-Droid ツ Jun 10 '14 at 11:33
  • i have published call recorder app and tested it on note 3, note2, note 1,galaxy grand quattro but in galaxy grand voice call doesnt work so i had used camcorder over there – Auto-Droid ツ Jun 10 '14 at 11:35
  • audioSource = MediaRecorder.AudioSource.CAMCORDER is working ... thankyou sooo much ... but the volume of incoming voice is very much low – user2064024 Jun 10 '14 at 11:39
  • Its using the mic but the interface is different it will have different effects on different devices try it out on various device you will see the change in voice volume – Auto-Droid ツ Jun 10 '14 at 11:41
  • @Auto how did you detect when an outgoing call is picked. – Ayush Bansal Mar 12 '17 at 11:28
  • @AyushBansal theres no way you can actually find the user as picked up the call, So you have to start recording on EXTRA_STATE_OFFHOOK and stop it on IDLE. downside is that you may end up recording ringing tone as well – Auto-Droid ツ Mar 12 '17 at 21:35
  • @Auto so do i use ```MediaRecorder.AudioSource.MIC``` , ```MediaRecorder.AudioSource.VOICE_CALL``` or something else to record outgoing call. – Ayush Bansal Mar 13 '17 at 11:29
  • @AyushBansal use MediaRecorder.AudioSource.VOICE_CALL for recording but give and option in setting for user to select the audio source {VOICE_CALL, MIC, UPLINK, DOWNLINK} so if the user device doesn't support voice_call he can change it to mic – Auto-Droid ツ Mar 13 '17 at 11:45
  • I am able to record caller voice by all 3 options MediaRecorder.AudioSource.MIC , MediaRecorder.AudioSource.CAMCORDER, MediaRecorder.AudioSource.VOICE_CALL.... but unable to record Reciever end voice... I tried in Moto E and Moto G4 Plus. – Imran Khan Saifi Sep 11 '17 at 12:41
  • @Auto-Droidツ Somehow this app has ability to detect answered stated even for outgoing calls : https://play.google.com/store/apps/details?id=com.boldbeast.recorder&hl=en https://stackoverflow.com/q/50926025/878126 – android developer Jun 21 '18 at 07:13
0

I think the issue that we don't listen the other side voice is for Accessibility access. Need to turn on the Accessibility for the recorder app. In that way issue will be fixed. Also use recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);

  • Do u have a working solution for this? @pushpo rani – Akshay Kumar Both Apr 08 '21 at 11:16
  • Try this one to add accessibility service in your app https://medium.com/@vanshikaa937/a-complete-guide-to-accessibility-service-part-1-c00387230019 After that add this to your app activity to enable accessibility from app https://stackoverflow.com/questions/38360198/programmatically-enabling-disabling-accessibility-settings-on-android-device – pushpo rani Apr 08 '21 at 13:41
-1

You cannot record the caller voice as long as the volume is low. It is programmatically not possible, because the AudioRecorder or MediaRecorder only recording through the microphone. The only thing You can do is, set the volume from the speaker as loud as possible.

Changing the AudioSoucre to .MIC will not help, there is an unfixed issue:

https://code.google.com/p/android/issues/detail?id=4075

This issue is not fixed until now and I think it will not be fixed in the future, because recording phone calls is not allowed in most countries

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
-1

An audio source of MIC should record incoming calls. You can set recording volume to the maximum level and turn on loudspeaker too as follows:

//before recording AudioManager audioManager;

    //turn on speaker
     audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
     audioManager.setSpeakerphoneOn(true);
    //increase Volume
     audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);

//start recording

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    File audioFile = File.createTempFile("temp", "3gp", path);
    recorder.setOutputFile(audioFile.getAbsolutePath());
    recorder.prepare();     
    recorder.start();   

Also there are other constants you can use with setAudioSource(), Just follow the guide to understand how each works

Nana Ghartey
  • 7,901
  • 1
  • 24
  • 26
  • yes by using Voice_Call and Voice_communication i got recorder failed exception at recorder.start() – user2064024 Jun 12 '14 at 05:28
  • That because the device you used doesn't support any of them. I'm using MIC in an app and it works fine. Does the issue happen even if you increase the system volume and turn on loudspeaker as I did in my answer? – Nana Ghartey Jun 12 '14 at 08:15
  • Have you tested on other devices as well? – Nana Ghartey Jun 12 '14 at 16:10
  • An audio source of MIC should record incoming calls -- Are you sure? – CopsOnRoad Mar 09 '18 at 14:23
  • @CopsOnRoad yes, I added "You can set recording volume to the maximum level and turn on loudspeaker" .. When you do that, calls can be recorded via the mic – Nana Ghartey Mar 09 '18 at 21:56
  • I know that. But this is not what developers generally want. – CopsOnRoad Mar 10 '18 at 06:28
  • Hi. I am facing the same problem as others. Tell me something, isn't turning on the speaker phone actually turns it on. I mean, the user notices this and will have to talk in the loudspeaker mode? – Manish Kumar Sharma Sep 20 '18 at 16:38