应用程序进入后台时保存视频文件[Camera2Api]

时间:2017-11-26 16:46:54

标签: android video background android-camera2 onpause

我有一个相机应用程序。我的问题是,当我正在录制并且应用程序进入bacground时,mediarecorder所拥有的文件无效。我想保存文件先保存。我尝试了单击停止按钮时使用的相同代码,但它不起作用。 有什么建议吗?

这是我的onPause()方法

@Override
public void onPause() {
    if (isRecording) {
        stopRecordingVideo();
        Log.d("YUI", "true");
        fileQueue.addToQueue(mCurrentFile);

    } else {
        Log.d("YUI", "false");
        mMediaRecorder = null;
        closeCamera();
    }
    counter--;
    stopBackgroundThread();
    //        if (mCurrentFile != null) {
    //            mCurrentFile.delete(); // delete empty file
    //        }
    super.onPause();
}
public void stopRecordingVideo() {
    stopRecordingVideo(false);
}
private void stopRecordingVideo(boolean kill) {
    // UI
    isRecording = false;

    // Stop Recording
    closeCamera();
    if (!kill) {
        openCamera(mCameraLayout.getWidth(), mCameraLayout.getHeight());
    }
}

private void closeCamera() {
    try {
        mCameraOpenCloseLock.acquire();
        if (mPreviewSession != null) {
            mPreviewSession.close();
            mPreviewSession = null;
        }
        if (mCameraDevice != null) {
            mCameraDevice.close();
            mCameraDevice = null;
        }
        if (mMediaRecorder != null) {
            mMediaRecorder.stop();
            mMediaRecorder.release();
            mMediaRecorder = null;
        }
    } catch (InterruptedException ie) {
        Crashlytics.log("closeCamera error");
        Crashlytics.logException(ie);
        ie.printStackTrace();
        getCam2Listener().onInterruptedException(ie);
        throw new RuntimeException("Interrupted while trying to lock camera closing.");
    } catch (Exception exc) {
        Crashlytics.log("closeCamera error");
        Crashlytics.logException(exc);
    }

    finally {
        mCameraOpenCloseLock.release();
    }
}

0 个答案:

没有答案