Android通话录音及状态码38错误解决

拨打一次电话会收到两次相同的广播。导致MediaRecorder会初始化两次。第二次初始化的时候报错崩溃。加一个为空判断就可以了
报状态码4错误。路径可能存在问题

电话状态判断代码

  public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent(context,CallRecorder.class);
        //电话管理者对象
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //得到电话状态
        int state = manager.getCallState();

        Bundle bundle = intent.getExtras();
        String num = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);//获取当前拨打电话的号码

        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE://挂断的状态
                context.stopService(intent1);
                Log.i("BRO","IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK://接听的状态
                Log.i("BRO","OFF");
                context.startService(intent1);
                break;
            case TelephonyManager.CALL_STATE_RINGING://响铃的状态
                System.out.println("响铃"+num);
                break;

            default:
                break;
        }

录音代码

 private void getAudio() {
        if (recorder != null) {
            Log.i("TAG", "notNull");
        } else {
            recorder = new MediaRecorder();
        }
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        //指定声音文件
        String path = Environment.getExternalStorageDirectory() + "/abc.amr";
        recorder.setOutputFile(path);
        //编码格式
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            recorder.prepare();
            recorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Android通话录音及状态码38错误解决)