无法在Android中录制来电和去电的音频 [英] Unable to record audio of Incoming and Outgoing phone call in Android

查看:141
本文介绍了无法在Android中录制来电和去电的音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在Android中记录来电和去电的音频

Unable to record audio of Incoming and Outgoing phone call in Android

我正在使用Broadcastreceiver来检测电话,它工作正常. 每当开始电话通话时,我都会使用以下代码开始记录电话通话,并创建一个"CALLLOG"文件夹,其中将存储每个通话记录.

I am using Broadcastreceiver for detecting Phonecalls, It is working fine. When ever phonecall is started I am using below code for start recording Phonecall and creating a folder of "CALLLOG", in which each call record will be stored.

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;
} 

以下用于停止记录的代码

Below code for stopping the record

public void stopRecording(){
        if (recordstarted) {
            recorder.stop();
            audioManager.setMode(AudioManager.MODE_NORMAL);
            recordstarted = false;
        }
    }

音频文件的扩展名是".amr".

The extension of audio files are ".amr".

以上代码未记录电话呼叫的音频,它正在创建"CALLLOG"文件夹,并且已存储".amr"文件,但未记录音频.我从2天开始对此进行了研究.

Above code is not recording the audio of a phonecall, it is creating a folder of "CALLLOG" and ".amr" files are stored but audio is not recording.I was working on this from 2 days.

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).

我无法记录来电和去电的音频,有时它会工作,有时却无法工作. 请帮我. 预先感谢.

I am unable to record audio of Incoming and outgoing calls.Some times it works, sometimes it is not working. Please help me on this. Thanks in Advance.

推荐答案

我过去在尝试在视频通话期间录制音频和视频时遇到了相同的问题.在通话过程中,其他进程正在使用MIC,因此在将MediaRecorder AudioSource设置为MIC之前,只需检查MIC实例是否可用.您可以像这样测试

I had the same issue in the past I was trying to record Audio + Video during video call. While device is in call MIC is being used by other processes, so before setting MediaRecorder AudioSource as MIC just check whether MIC instance is available or not. You can test it like that

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库,以下是链接. aykuttasil/CallRecorder 进行检查.

Simple solution of this problem is to use some CallRecorder Library following is the link. aykuttasil/CallRecorder check it.

这篇关于无法在Android中录制来电和去电的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆