调用MediaRecorder.stop()时冻结应用程序

时间:2014-11-11 12:03:54

标签: android video record

我有使用SurfaceView的活动,我希望在此活动中启动和停止视频记录,但如果我转到其他活动,我想要继续录制视频。现在我开始录制视频(在活动1中)并转到其他活动,当我回到(活动1)并想要停止录制视频时,我的应用程序在被叫recorder.stop();时冻结我如何解决它?

Init SurfaceView和SurfaceHolder:

SurfaceView cameraView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

开始录制视频:

 protected void startRecordVideo() {
    if (cam == null) {
        cam = Camera.open();
        try {
            cam.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    recording = true;
    recorder = new MediaRecorder();
    recorder.setCamera(cam);
    initRecorder();
    prepareRecorder();
    try {
        cam.unlock();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    recorder.start();
}

停止录制视频(这是应用程序冻结):

 protected void stopRecordVideo() { //cam - Camera, recorder - MediaRecorder
    mc.IS_RECORD = false;
    recording = false;
    try {
        cam.lock(); 
        recorder.stop(); //app freezes in this line (i used Log)
        recorder = null;
        cam.reconnect();
        cam.release();
        cam = null;


    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

P.S。我不使用camera.startPreview();camera.stopPreview();

1 个答案:

答案 0 :(得分:0)

尝试将stopRecordVideo包裹在Thread内,因此它不会阻止主(UI)Thread

如果它不起作用,请看一下这个问题:

Android Media Recording using threads

看来你必须做一点" hack"