通话录音应用无法在7.0版以上的Android系统中使用

时间:2019-03-15 05:27:08

标签: java android

我正在开发通话记录应用程序,该应用程序在Android OS版本6.0之前都可以正常运行,但在OS版本7.0及更高版本上它停止记录来电语音。我根据设备要求同时使用MediaRecorder.AudioSource.VOICE_CALLMediaRecorder.AudioSource.VOICE_COMMUNICATION。在下面提供代码

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

我已在android清单中启用了权限,但是它需要CAPTURE_AUDIO_OUTPUT权限才能在两端录制语音,这是系统级权限,因此我在授予此类权限时遇到困难。请为我建议一种授予此类权限的方法,或在所有设备中启用MediaRecorder.AudioSource.VOICE_CALL方法的任何替代方法。

1 个答案:

答案 0 :(得分:2)

从Android M及更高版本开始,您需要在运行时征求权限。您可以在清单中将权限声明为

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

然后在启动器活动的onCreate期间要求相同。检查此link以获得详细说明。另外,请确保使用VOICE_COMMUNICATION而不是现在不推荐使用的VOICE_CALL