0

我从 MediaRecorder start() 方法中得到这个代码。该文件是在 SD 卡上的正确位置创建的,开始预览,等等......当我从我的代码中注释掉这个方法时,一切正常。

这是我的 CamcoderPreview 类:

public class CamcorderPreview extends SurfaceView implements
    SurfaceHolder.Callback {

MediaRecorder recorder;
SurfaceHolder holder;
String fileName;
Camera camera;

private int counter = 1;

public CamcorderPreview(Context context, AttributeSet attrs) {
    super(context, attrs);
    holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    recorder = new MediaRecorder();

}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    initCamera();
}

private void initCamera(){
    camera = Camera.open();

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        System.out.println("error on camera init:" + e);
        e.printStackTrace();
    }

    camera.lock();
    recorder.setCamera(camera);

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_HH_mm");

    File dir = new File("/sdcard/TrainHardVideo/" + sdf.format(new Date()));
    if (!dir.exists()) {
        dir.mkdirs();
    }

    recorder.setOutputFile(dir.getAbsolutePath() + "/" + counter + ".mp4");
     recorder.setPreviewDisplay(holder.getSurface());

    if (recorder != null) {
        try {
            recorder.prepare();
            counter++;
            System.out.println("recorder prepare...");
        } catch (IllegalStateException e) {
            Log.e("IllegalStateException", e.toString());
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        }

        camera.startPreview();
        recorder.start();
    }

}


public void surfaceCreated(SurfaceHolder holder) {

}

public void surfaceDestroyed(SurfaceHolder holder) {
}

public MediaRecorder getRecorder() {
    return recorder;
}

}

这是 LogCat 中的消息:

11-03 10:06:06.449: E/MediaRecorder(4104): start failed: -19
11-03 10:06:06.453: D/AndroidRuntime(4104): Shutting down VM
11-03 10:06:06.453: W/dalvikvm(4104): threadid=1: thread exiting with uncaught exception (group=0x40a451f8)
11-03 10:06:06.457: E/AndroidRuntime(4104): FATAL EXCEPTION: main
11-03 10:06:06.457: E/AndroidRuntime(4104): java.lang.RuntimeException: start failed.
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.media.MediaRecorder.native_start(Native Method)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.media.MediaRecorder.start(MediaRecorder.java:714)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at hu.android.TrainHardPro.CamcorderPreview.initCamera(CamcorderPreview.java:90)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at hu.android.TrainHardPro.CamcorderPreview.surfaceChanged(CamcorderPreview.java:44)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.view.SurfaceView.updateWindow(SurfaceView.java:544)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.view.SurfaceView.access$000(SurfaceView.java:81)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1617)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.os.Looper.loop(Looper.java:137)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at android.app.ActivityThread.main(ActivityThread.java:4575)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at java.lang.reflect.Method.invoke(Method.java:511)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-03 10:06:06.457: E/AndroidRuntime(4104):     at dalvik.system.NativeStart.main(Native Method)

谢谢!

4

0 回答 0