Voice Call recording in android using MediaRecorder

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?


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.


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


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

链接地址: http://www.djcxy.com/p/75464.html

上一篇: GHC / Haskell平台安装的地方

下一篇: 使用MediaRecorder在Android中进行语音通话记录