欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android自动通话录音实现_手机双向通话录音开发源代码

程序员文章站 2022-03-01 12:52:02
...

最近开发的项目中需要的的一个功能、百度上找了很久才搞出来、最近逆境千辛万苦才整好了一个双向录音、现在分享给大家、另外由于录音是基于一个PhoneService做的、所以需要打开本Demo之后、先打个电话才能录音的、最后才能听到效果、下面是运行的效果图

Android自动通话录音实现_手机双向通话录音开发源代码


MainActivity中onCreate启动Service

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViews();
    Intent service = new Intent(this, PhoneService.class);
    startService(service); // 启动服务
}


PhoneStateListener代码

private final class PhoneListener extends PhoneStateListener {
    private String incomeNumber; // 来电号码
    private MediaRecorder mediaRecorder;
    private File file;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        try {
            System.out.println(">>>>>>CallState>>>>>>>>"   state);
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING: // 来电
                System.out.println(">>>>>>来电>>>>>>>>"   state);
                this.incomeNumber = incomingNumber;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK: // 接通电话
                file = new File(Environment.getExternalStorageDirectory(),
                        this.incomeNumber   System.currentTimeMillis()
                                  ".3gp");
                System.out.println(">>>>>>接通>>>>>>>>"   state);
                mediaRecorder = new MediaRecorder();
                // mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                //获得声音数据源(下麦克风)
                mediaRecorder
                        .setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);//这个设置就是获取双向声音
                mediaRecorder
                        .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 按3gp格式输出
                mediaRecorder
                        .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mediaRecorder.setOutputFile(file.getAbsolutePath()); // 输出文件
                mediaRecorder.prepare(); // 准备
                mediaRecorder.start();
                break;

            case TelephonyManager.CALL_STATE_IDLE: // 挂掉电话
                System.out.println(">>>>>>挂电话>>>>>>>>"   state);
                if (mediaRecorder != null) {

                    System.out.println(">>>>"
                              Environment.getExternalStorageDirectory());
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    mediaRecorder = null;
                    AppliacationIMmpl.file = file;
                }

                break;

            }
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    }


广播监听启动

public class PhoneBroadcastReceiver extends BroadcastReceiver  
{  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        System.out.println(">>PhoneBroadcastReceiver>>" Environment.getExternalStorageDirectory());
        Intent service = new Intent(context, PhoneService.class);  
        context.startService(service);   //启动服务  
    }  
  
} 


源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1eQ8Ze3w 密码: f2x7