如何修复MediaRecorder启动失败:Android 7.1.1中的-2147483648

时间:2017-12-22 11:14:58

标签: android runtimeexception android-mediarecorder android-7.1-nougat

我正在开发一个用于录制通话的Android应用。

当我在 Android 7.1.1(诺基亚2)

中运行应用时

MediaRecorder start failed: -2147483648 RuntimeException已获得。

当我开始新的调用时发生了异常。此异常的完整Logcat是,

E/MediaRecorder: prepare() E
E/MediaRecorder: prepare() new RandomAccessFile E
E/MediaRecorder: prepare() new RandomAccessFile X
E/MediaRecorder: prepare() _setOutputFile(file.getFD() E
E/MediaRecorder: prepare() _setOutputFile(file.getFD() x
E/MediaRecorder: prepare() x
E/MediaRecorder: start failed: -2147483648
E/RecordService: java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.onbit.mob.cal.RecordService$CallBr.startRecording (RecordService.java:446)
at com.onbit.mob.cal.RecordService$CallBr.onReceive (RecordService.java:377)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1122)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

与此例外相关的代码段是,

recorder = new MediaRecorder();
recorder.setAudioSource(VOICE_COMMUNICATION);
recorder.setAudioSamplingRate(44100);
recorder.setAudioEncodingBitRate(320000);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
    recorder.prepare();
} catch (IllegalStateException e) {
    Log.e(TAG, Log.getStackTraceString(e));
} catch (IOException e) {
    Log.e(TAG, Log.getStackTraceString(e));
}
try {
    recorder.start();
} catch (Exception e){
    Log.e(TAG , Log.getStackTraceString(e));
}

任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

对我来说,MediaRecorder 启动失败:-2147483648 错误 取决于设备和操作系统级别。通常,录音机 将启动 1 次,随后的启动将失败。 修复方法是添加对 MediaRecorder.release() 的调用。

...
try {
      audioRecorderType_MR.stop();
      audioRecorderType_MR.release();  // <<-- ADD THIS LINE
} catch (Exception e) {
      J42CallerId.printStackTrace(new J42ProgramCheck("J42VM0072E:", e));
}
audioRecorderType_MR = null;
...