我正在尝试使用媒体记录器在Android中录制视频。当我在2.3.4版本的媒体录像机中尝试视频录制时,它在4.0及更高版本中工作正常,启动失败例外-12。
这是我的媒体录制器代码。
mrec.setCamera(camera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mrec.setVideoSize(getMaxSupportedVideoSize().width,
getMaxSupportedVideoSize().height);
mrec.setOutputFile(path + filename);
mrec.setMaxDuration(30000);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.prepare();
mrec.start();
我得到的运行时异常是
01-21 12:58:16.459: E/MediaRecorder(2461): start failed: -12
01-21 12:58:16.469: W/System.err(2461): java.lang.RuntimeException: start failed.
01-21 12:58:16.469: W/System.err(2461): at android.media.MediaRecorder.native_start(Native Method)
01-21 12:58:16.469: W/System.err(2461): at android.media.MediaRecorder.start(MediaRecorder.java:603)
01-21 12:58:16.469: W/System.err(2461): at com.roseenvy.ui.activities.VideoRecordActivity.startRecording(VideoRecordActivity.java:235)
01-21 12:58:16.469: W/System.err(2461): at com.roseenvy.ui.activities.VideoRecordActivity.onClick(VideoRecordActivity.java:125)
01-21 12:58:16.469: W/System.err(2461): at android.view.View.performClick(View.java:2485)
01-21 12:58:16.469: W/System.err(2461): at android.view.View$PerformClick.run(View.java:9080)
01-21 12:58:16.469: W/System.err(2461): at android.os.Handler.handleCallback(Handler.java:587)
01-21 12:58:16.469: W/System.err(2461): at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 12:58:16.469: W/System.err(2461): at android.os.Looper.loop(Looper.java:130)
01-21 12:58:16.469: W/System.err(2461): at android.app.ActivityThread.main(ActivityThread.java:3737)
01-21 12:58:16.469: W/System.err(2461): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 12:58:16.469: W/System.err(2461): at java.lang.reflect.Method.invoke(Method.java:507)
01-21 12:58:16.469: W/System.err(2461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
01-21 12:58:16.469: W/System.err(2461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
01-21 12:58:16.469: W/System.err(2461): at dalvik.system.NativeStart.main(Native Method)
请帮助我如何解决这个问题。
提前致谢
答案 0 :(得分:2)
The below code worked for me.
public Size getMaxSupportedVideoSize() {
int maximum = 0;
int position = -1;
for (int i = 0; i < sizes.size(); i++) {
if (sizes.get(i).width > maximum) {
maximum = sizes.get(i).width; // new maximum
position = i;
}
}
if (position == -1) {
return null;
}
return sizes.get(position);
}
mrec = new MediaRecorder();
camera.unlock();
mrec.setCamera(camera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
Size maxSupportedVideoSize = getMaxSupportedVideoSize();
if (maxSupportedVideoSize == null) {
mrec.setVideoSize(480, 640);
} else {
mrec.setVideoSize(maxSupportedVideoSize.width,
maxSupportedVideoSize.height);
System.out.println(maxSupportedVideoSize.width + " width");
System.out.println(maxSupportedVideoSize.height + " height");
}
mrec.setOutputFile(path + filename);
mrec.setMaxDuration(30000);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
答案 1 :(得分:0)
只需评论此行,然后尝试。
mrec.setVideoSize(getMaxSupportedVideoSize().width,
getMaxSupportedVideoSize().height);
并更改此行::
mrec.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
请参阅此link如何使用MediaRecorder抓取视频。
希望这会有所帮助:)