我有一份工作和平的代码。它是在三星Android Camera EK-GC110(Android 4.1.2)上编写和测试的,效果很好。
代码启动新的Media Recorder实例并准备进行视频录制:
当我在Nexus 5(Android 5.0.1)手机上运行应用程序时,我得到:启动失败:-19错误并清空mp4文件。
代码:
mediaRecorder = new MediaRecorder();
camera.unlock();
long duration = calculateDuration();
MAX_DURATION = (int)duration/1000;
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setCamera(camera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncodingBitRate(5000000);
mediaRecorder.setVideoFrameRate(20);
mediaRecorder.setVideoSize(1280,720);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setMaxDuration((int)duration); // Set max duration
mediaRecorder.setOnInfoListener(this);
String root = getSDCardPath();
directory = new File(root + "/FileUpload/");
if (!directory.exists())
directory.mkdirs();
long currentTime = System.currentTimeMillis();
String uniqueOutFile = root + "/FileUpload/"
+ timeFormatter.format(currentTime)
+ ".mp4";
File outFile = new File(directory, uniqueOutFile);
if (outFile.exists()) {
outFile.delete();
}
mediaRecorder.setOutputFile(uniqueOutFile);
try {
mediaRecorder.prepare();
} catch (Exception e) {
}
mediaRecorder.start();
错误堆叠:
02-25 11:04:04.186 22334-22334/com.vp.fileupload.app I/Process﹕ Sending signal. PID: 22334 SIG: 9
02-25 11:04:05.467 22470-22470/com.vp.fileupload.app I/MediaRecorderJNI﹕ prepare: surface=0xaf251400
02-25 11:04:05.557 22470-22470/com.vp.fileupload.app E/MediaRecorder﹕ start failed: -19
02-25 11:04:05.564 22470-22470/com.vp.fileupload.app D/AndroidRuntime﹕ Shutting down VM
02-25 11:04:05.567 22470-22470/com.vp.fileupload.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vp.fileupload.app, PID: 22470
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.vp.fileupload.app.BackgroundVideoRecorder.cameraStart(BackgroundVideoRecorder.java:178)
at com.vp.fileupload.app.BackgroundVideoRecorder.surfaceCreated(BackgroundVideoRecorder.java:85)
at android.view.SurfaceView.updateWindow(SurfaceView.java:579)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:176)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1956)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:1)
我认为您的问题是硬件不支持20 fps的捕获。尝试删除fps的设置,看看它是否有效。
答案 1 :(得分:0)
尝试设置个人资料而不是自定义记录参数。
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));