android - 无法在 Android 中录制来电和去电的音频
问题描述
无法在 Android 中录制来电和去电的音频
我正在使用 Broadcastreceiver 来检测电话,它工作正常。当电话开始时,我使用下面的代码开始记录电话并创建一个“CALLLOG”文件夹,其中将存储每个通话记录。
public void startRecordingStoreFile(){
String out = new SimpleDateFormat("dd-MM-yyyy_hh-mm-ss").format(new Date());
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/CALLLOG");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = "Rec_"+out;
try {
audiofile = File.createTempFile(file_name, ".amr", sampleDir);
} catch (IOException e) {
e.printStackTrace();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
recordstarted = true;
}
下面的代码用于停止记录
public void stopRecording(){
if (recordstarted) {
recorder.stop();
audioManager.setMode(AudioManager.MODE_NORMAL);
recordstarted = false;
}
}
音频文件的扩展名为“.amr”。
上面的代码没有录制电话的音频,它正在创建一个“CALLLOG”文件夹,并且存储了“.amr”文件但没有录制音频。我从 2 天开始就一直在做这个。
For example suppose lets say I am calling to "X" person,
1.MIC is not recording once the "X"(other) person lift the call, until then audio is recording some times,
2.Some times MIC instance is not available as mentioned below solution by Afsar,
I have tried with below code but it doesn't work(Sometimes it works, sometimes not).
我无法录制来电和去电的音频。有时它可以工作,有时它不工作。请帮助我。提前致谢。
解决方案
过去我在视频通话期间尝试录制音频+视频时遇到了同样的问题。当设备处于通话状态时,其他进程正在使用 MIC,因此在将 MediaRecorder AudioSource 设置为 MIC 之前,只需检查 MIC 实例是否可用。你可以这样测试
private boolean validateMicAvailability(){
Boolean available = true;
AudioRecord recorder =
new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_DEFAULT, 44100);
try{
if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
available = false;
}
recorder.startRecording();
if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
recorder.stop();
available = false;
}
recorder.stop();
} finally{
recorder.release();
recorder = null;
}
return available;
}
这个问题的简单解决方案是使用一些 CallRecorder Library 下面是链接。 aykuttasil/CallRecorder检查它。
推荐阅读
- javascript - 为什么 resolve() 必须在提交之前发生?
- python - 将浮点数转换为 IEEE754
- image-processing - 关于 Unet 分段架构中的下采样
- php - MySQL 字符集和 PHP
- javascript - 打开按钮时停止图像移动
- mean.io - 我是否缺少 MEAN.io 的依赖项?
- c# - 使用 System.Linq.Dynamic.Core 查询具有成员字典的对象列表时无法获得多个结果
- python - python将文件从macos文件系统移动到共享windows服务器
- java - 如何防止 Spring Tool Suite 在每次编译时在控制台中添加额外的数据/详细信息
- php - 不同页面的不同标志