3

我正在尝试使用媒体记录器在 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)

请帮助我如何解决这个问题。

提前致谢

4

2 回答 2

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());
于 2013-03-22T06:27:07.540 回答
0

只需评论这一行,然后尝试。

mrec.setVideoSize(getMaxSupportedVideoSize().width,
            getMaxSupportedVideoSize().height);

并更改此行::

mrec.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

请参阅此链接如何使用 MediaRecorder 抓取视频。

希望这可以帮助 :)

于 2013-01-21T08:53:23.710 回答