简体   繁体   中英

Call Recording App not working in android above OS version 7.0

I am developing call recording app which is working fine upto OS version 6.0 of android but it stops recording incoming call voice on and above OS version 7.0. I am using MediaRecorder.AudioSource.VOICE_CALL and MediaRecorder.AudioSource.VOICE_COMMUNICATION both as per device requirement. Providing code below,

private boolean startMediaRecorder(int audioSource){
    recorder = new MediaRecorder();
    try{
        recorder.reset();
        recorder.setAudioSource(audioSource);
        recorder.setAudioSamplingRate(8000);
        recorder.setAudioEncodingBitRate(12200);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        fileName = FileHelper.getFilename(phoneNumber,type,getApplicationContext());
        recorder.setOutputFile(fileName);

        OnErrorListener errorListener = new OnErrorListener() {
            public void onError(MediaRecorder arg0, int arg1, int arg2) {
                Log.e(Constants.TAG, "OnErrorListener " + arg1 + "," + arg2);
                terminateAndEraseFile();
            }
        };
        recorder.setOnErrorListener(errorListener);

        OnInfoListener infoListener = new OnInfoListener() {
            public void onInfo(MediaRecorder arg0, int arg1, int arg2) {
                Log.e(Constants.TAG, "OnInfoListener " + arg1 + "," + arg2);
                terminateAndEraseFile();
            }
        };
        recorder.setOnInfoListener(infoListener);

        recorder.prepare();
        // Sometimes prepare takes some time to complete
        Thread.sleep(2000);
        recorder.start();
        recording = true;
        return true;
    }catch (Exception e){
        e.getMessage();
        return false;
    }
}

private void startRecording(Intent intent) {
    Log.d(Constants.TAG, "RecordService startRecording");
    boolean exception = false;


    if (!startMediaRecorder(MediaRecorder.AudioSource.VOICE_CALL)){
        if(startMediaRecorder(MediaRecorder.AudioSource.MIC)){
            audioManager =(AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.setMode(AudioManager.MODE_IN_CALL);
            audioManager.setStreamVolume(3,audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),0);
            Intent intent1 = new Intent(getBaseContext(), DialogConfirmActivity.class);
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent1);
        }else{
            exception = true;
        }
    }

    if (exception) {
        terminateAndEraseFile();
    }

    if (recording) {
        Toast toast = Toast.makeText(this,
                this.getString(R.string.receiver_start_call),
                Toast.LENGTH_SHORT);
        toast.show();
    } else {
        Toast toast = Toast.makeText(this,
                this.getString(R.string.record_impossible),
                Toast.LENGTH_LONG);
        toast.show();
    }
}

I have enabled permissions in android manifests but it requires CAPTURE_AUDIO_OUTPUT permission to record voice at both end which is system level permission so I am facing difficulties in granting such permission. Please suggest me a way to grant such permission or any alternate way to enable MediaRecorder.AudioSource.VOICE_CALL method in all devices.

From Android M and above you need to ask permission at run-time. You can declare the permission in Manifest as,

<uses-permission android:name="android.permission.RECORD_AUDIO" />

And then ask for the same during onCreate of your launcher activity. Check this link for detailed explanation. Also make sure that you use VOICE_COMMUNICATION instead of VOICE_CALL which is deprecated now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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