我正在打电话记录。 以下代码在API级别25之前正常工作。
但是,在录制来电时, 异常(" IllegalStateException") 仅在 API级别26 中发生
01-01 19:27:08.715 5446-5446/com.app E/MediaRecorder: start failed: -38
01-01 19:27:08.716 5446-5446/com.app W/System.err: java.lang.IllegalStateException
01-01 19:27:08.717 5446-5446/com.app W/System.err: at android.media.MediaRecorder.start(Native Method)
01-01 19:27:08.718 5446-5446/com.app W/System.err: at com.app.CallRecorderManager.startRecording(CallRecorderManager.java:62)
01-01 19:27:08.718 5446-5446/com.app W/System.err: at com.app.PhoneListener.onCallStateChanged(PhoneListener.java:59)
01-01 19:27:08.719 5446-5446/com.app W/System.err: at android.telephony.PhoneStateListener$1.handleMessage(PhoneStateListener.java:324)
01-01 19:27:08.721 5446-5446/com.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:105)
01-01 19:27:08.721 5446-5446/com.app W/System.err: at android.os.Looper.loop(Looper.java:164)
01-01 19:27:08.722 5446-5446/com.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
01-01 19:27:08.722 5446-5446/com.app W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-01 19:27:08.723 5446-5446/com.app W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
01-01 19:27:08.724 5446-5446/com.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的代码:
public void startRecording(String outputPath) throws Exception {
File director = new File(outputPath);
if (!director.exists()) {
if (!director.mkdirs()) {
throw new IOException("Can not create directory ");
}
}
myAudioRecorder = new MediaRecorder();
myAudioRecorder.reset();
if (Build.VERSION.SDK_INT <= 22) {
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
} else {
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
}
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
fileName = outputPath + "/" + Filename();
myAudioRecorder.setOutputFile(fileName);
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
myAudioRecorder.setOnErrorListener(errorListener);
} catch (Exception e){
throw new Exception(e.getMessage());
}
}