简体   繁体   English

使用 MediaRecorder 在 android 中进行语音通话录音

[英]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.我在录制通话时遇到问题,我进行了一项服务并调用了BroadcastReceiver以获取通话状态。 In TelephonyManager.EXTRA_STATE_OFFHOOK when the call is received.TelephonyManager.EXTRA_STATE_OFFHOOK中收到呼叫时。 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();在上面的代码中,它在 recoder.start() 上抛出了 recoder 失败的异常;

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一年前我也有同样的疑问AudioSource.VOICE_CALL 不在 android 4.0 中工作,但在 android 2.3 中工作

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 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.适用于有限的设备,它只会在不支持语音呼叫的设备上给出异常,因此捕获异常并重新从麦克风开始录音,这将使您在不受支持的设备中保持安全。

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);也使用 recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);

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.这在编程上是不可能的,因为 AudioRecorder 或 MediaRecorder 只能通过麦克风录音。 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:将 AudioSoucre 更改为 .MIC 无济于事,有一个未解决的问题:

https://code.google.com/p/android/issues/detail?id=4075 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. MIC 的音频源应记录来电。 You can set recording volume to the maximum level and turn on loudspeaker too as follows:您可以将录音音量设置为最大级别并打开扬声器,如下所示:

//before recording AudioManager audioManager; //录制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还有其他常量可以与 setAudioSource() 一起使用,只需按照指南了解每个常量的工作原理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM