解决AndroidCamera2官方Demo的bug

来源:互联网 发布:超级优化基因液txt网盘 编辑:程序博客网 时间:2024/05/19 06:17

做Android开发好多年了,也没有想过要写博客,一直都是伸手党。现如今因公司业务需要,需要自定义相机,就从官方下载了个Demo准备研究一下,谁知道官方大神代码在录制视频执行stopRecordingVideo方法是报错,没办法只有边查资料边研究。其实最后的原因是因为没有释放Session的会话而造成的,具体的解决方案我贴出来,如果有人看不明白可以咨询我或者留言。

最终解决方案:

先修改stopRecordingVideo方法:

private void stopRecordingVideo() {        // UI        mIsRecordingVideo = false;        mButtonVideo.setText(R.string.record);        /**         * 对预览的Session进行释放,这里我们进行异常的捕获。         */        try {            mPreviewSession.stopRepeating();            mPreviewSession.abortCaptures();        } catch (CameraAccessException e) {            e.printStackTrace();        }        // Stop recording//        mMediaRecorder.stop();//        mMediaRecorder.reset();        /**         * stop会抛出异常,这里我们进行捕获,如果MediaRecorder调用停止方法异常,         * 我们就需要释放对象。否则MediaRecorder不停止再次录制会报错。         */        try {            mMediaRecorder.stop();            mMediaRecorder.reset();        }        catch (Exception e) {            e.printStackTrace();            mMediaRecorder = null;        }        Activity activity = getActivity();        if (null != activity) {            Toast.makeText(activity, "Video saved: " + mNextVideoAbsolutePath,                    Toast.LENGTH_SHORT).show();            Log.d(TAG, "Video saved: " + mNextVideoAbsolutePath);        }        mNextVideoAbsolutePath = null;        startPreview();    }
接着修改setUpMediaRecorder方法:

private void setUpMediaRecorder() throws IOException {    final Activity activity = getActivity();    if (null == activity) {        return;    }    /**     * 因为MediaRecorder在捕获异常的时候我们释放了,这里如果不重新new的话会报空指针。     * 所以这里我们也要修改一下。     */    if (mMediaRecorder == null){        mMediaRecorder = new MediaRecorder();        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);    } else {        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);    }    if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {        mNextVideoAbsolutePath = getVideoFilePath(getActivity());    }    mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);    mMediaRecorder.setVideoEncodingBitRate(10000000);    mMediaRecorder.setVideoFrameRate(30);    mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();    switch (mSensorOrientation) {        case SENSOR_ORIENTATION_DEFAULT_DEGREES:            mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));            break;        case SENSOR_ORIENTATION_INVERSE_DEGREES:            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));            break;    }    mMediaRecorder.setOrientationHint(0);    mMediaRecorder.prepare();}


原创粉丝点击