startVideoRecording method

Future<void> startVideoRecording({
  1. onLatestImageAvailable? onAvailable,
  2. bool enablePersistentRecording = true,
})

Start a video recording.

You may optionally pass an onAvailable callback to also have the video frames streamed to this callback.

The video is returned as a XFile after calling stopVideoRecording. Throws a CameraException if the capture fails.

enablePersistentRecording parameter configures the recording to be a persistent recording. A persistent recording can only be stopped by explicitly calling stopVideoRecording and will ignore events that would normally cause recording to stop, such as lifecycle events or explicit calls to setDescription while recording is in progress. Currently a no-op on platforms other than Android.

Implementation

Future<void> startVideoRecording({
  onLatestImageAvailable? onAvailable,
  bool enablePersistentRecording = true,
}) async {
  _throwIfNotInitialized('startVideoRecording');
  if (value.isRecordingVideo) {
    throw CameraException(
      'A video recording is already started.',
      'startVideoRecording was called when a recording is already started.',
    );
  }

  void Function(CameraImageData image)? streamCallback;
  if (onAvailable != null) {
    streamCallback = (CameraImageData imageData) {
      onAvailable(CameraImage.fromPlatformInterface(imageData));
    };
  }

  try {
    await CameraPlatform.instance.startVideoCapturing(
      VideoCaptureOptions(
        _cameraId,
        streamCallback: streamCallback,
        enablePersistentRecording: enablePersistentRecording,
      ),
    );
    value = value.copyWith(
      isRecordingVideo: true,
      isRecordingPaused: false,
      recordingOrientation: Optional<DeviceOrientation>.of(
        value.lockedCaptureOrientation ?? value.deviceOrientation,
      ),
      isStreamingImages: onAvailable != null,
    );
  } on PlatformException catch (e) {
    throw CameraException(e.code, e.message);
  }
}