无法为媒体记录器设置视频质量

时间:2013-03-21 07:24:41

标签: android video video-capture

在我的代码中,我无法设置camcoder配置文件

private void initRecoreder() {
    File dir = new File(Environment.getExternalStorageDirectory().getPath()
            + "/Registrator/Video/");

    recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setMaxDuration(900000);// 15 min
    if (!dir.exists()) {
        dir.mkdirs();
    }
    recorder.setOutputFile(dir.getAbsolutePath() + "video.3gp");
}

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    prepareRecorder();
    initRecoreder();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();
}

这是我的logcat

03-21 11:16:38.660: E/AndroidRuntime(4584): FATAL EXCEPTION: main
03-21 11:16:38.660: E/AndroidRuntime(4584): java.lang.RuntimeException: Unable to start activity ComponentInfo{mobi.esys.videoregistrator/mobi.esys.videoregistrator.RegistatratorActivity}: java.lang.IllegalStateException
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.os.Looper.loop(Looper.java:137)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.ActivityThread.main(ActivityThread.java:4507)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at java.lang.reflect.Method.invoke(Method.java:511)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at dalvik.system.NativeStart.main(Native Method)
03-21 11:16:38.660: E/AndroidRuntime(4584): Caused by: java.lang.IllegalStateException
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.media.MediaRecorder.setOutputFormat(Native Method)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at mobi.esys.videoregistrator.RegistatratorActivity.initRecoreder(RegistatratorActivity.java:52)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at mobi.esys.videoregistrator.RegistatratorActivity.onCreate(RegistatratorActivity.java:34)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.Activity.performCreate(Activity.java:4469)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
03-21 11:16:38.660: E/AndroidRuntime(4584):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
03-21 11:16:38.660: E/AndroidRuntime(4584):     ... 11 more
03-21 11:17:02.930: E/MediaRecorder(4671): setOutputFile called in an invalid state(1)

2 个答案:

答案 0 :(得分:0)

使用此代码录制视频。

       recorder = new MediaRecorder();

          recorder.setCamera(mCamera);
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 


            profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

            recorder.setProfile(profile);

            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setOutputFile(path+filename);
            prepareRecorder();
            recorder.start();

并在清单

中添加此权限
  <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

答案 1 :(得分:0)