Android - 录制视频后停止并重置相机时的致命信号11

时间:2016-04-21 21:48:05

标签: android android-camera sigsegv android-mediarecorder

我目前有一个应用程序,我正在尝试录制视频,使用以下方法初始化相机:

private void initRecorder(Surface surface) throws IOException {
    if (mCamera == null) {
        mCamera = Camera.open();
        mCamera.unlock();
    }

    if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setPreviewDisplay(surface);
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoSize(640, 480);
    mMediaRecorder.setOutputFile(videoFile);

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

    initSuccess = true;
}

当我尝试停止相机或调用surfaceDestroyed时,我调用以下方法:

private void resetCamera()  {
    mMediaRecorder.stop();
    mMediaRecorder.reset();
    mMediaRecorder.release();
    mCamera.release();

    mCamera = null;

    try {
        initRecorder(mHolder.getSurface());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是,在此代码的某处,我收到致命信号11错误。显然,代码试图非法访问部分内存。

我在想,我怎么能解决这个错误?

这是从我开始录制视频到停止录制时的Logcat转储:

D/dalvikvm: GC_FOR_ALLOC freed 1309K, 27% free 7434K/10168K, paused 9ms, total 9ms
I/dalvikvm: Could not compile trace for Ljava/util/Arrays;fill, offset 5
I/dalvikvm: ++++++++++++++++++++++++++++++++++++++++++++
I/dalvikvm: JIT_INFO: ME Issues while compiling trace  Ljava/util/Arrays;fill, offset 5
I/dalvikvm: The trace provoked a spill.
I/dalvikvm: Trying less registerization from 1 to 0
D/dalvikvm: GC_FOR_ALLOC freed 1415K, 27% free 7438K/10168K, paused 10ms, total 10ms
A/libc: Fatal signal 11 (SIGSEGV) at 0x00000010 (code=1), thread 13774 (arch.treadmill3)

1 个答案:

答案 0 :(得分:0)

在mMediaRecorder.release()之后;你必须写mMediaRecorder = null;因为release()方法似乎有很少的bug。 像这样:

if (mMediaRecorder != null) {
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mMediaRecorder = null;
    }