MediaRecorder.start()有时会抛出java.lang.RuntimeException:start failed

时间:2018-06-06 13:00:40

标签: android android-mediarecorder

我有Android应用程序。通过MediaRecorder录制视频,有时MediaRecorder.start()录制的视频会因此例外而失败:

java.lang.RuntimeException: start failed.
    at android.media.MediaRecorder._start(Native Method)
    at android.media.MediaRecorder.start(MediaRecorder.java:946)
    at com.example.example.RecordActivity.aeU(Unknown Source)
    at com.example.example.RecordActivity.p(Unknown Source)
    at com.example.example.RecordActivity$23.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)    

它独立于设备和Android版本(从Android 5到8.1)。失败是非常随机的。例如,前10个MediaRecorder启动是正常的,11。使用相同的设置抛出异常,我不知道为什么。从总的MediaRecorder启动将只失败几次,例如只有2%,但在某些设备上失败更频繁。这是某些设备上的设备错误或Android错误?我能减少异常吗?我在MediaRecorder.prepare()或其他之后等待100毫秒?有这个问题的经验吗?

Java代码:

//c is Camera: Camera.open(cameraId)

MediaRecorder mr = new MediaRecorder();

//call methods in this order!
c.lock();
c.unlock();

//1st. Initial state:
mr.setCamera(c);

//2st. Initialized source:
if (recordAudio)
    mr.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);

mr.setVideoSource(MediaRecorder.VideoSource.CAMERA);

//3st. config from profile:
CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_BACK, CamcorderProfile.QUALITY_HIGH); //or other quality

mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

//Audio
if(recordAudio) {
    mr.setAudioEncoder(profile.audioCodec);   
    mr.setAudioEncodingBitRate(profile.audioBitRate);  
    mr.setAudioChannels(profile.audioChannels);
    mr.setAudioSamplingRate(profile.audioSampleRate);
}

//Video
mr.setVideoEncoder(profile.videoCodec);
mr.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
mr.setVideoFrameRate(profile.videoFrameRate);
mr.setVideoEncodingBitRate(profile.videoBitRate);
mr.setOrientationHint(needRotation);
mr.setOutputFile(videoFile.toString());
mr.setPreviewDisplay(sur);

mMediaRecorder.prepare();
mMediaRecorder.start();

0 个答案:

没有答案