An Tran
An Tran

Reputation: 154

How to switch camera while recording video with camera package in Flutter?

I can switch between the front and back cameras if I'm not recording video. But while recording video the camera preview widget goes black for a short while and then shows the same camera, not switching to the new camera.
I'm working with Flutter 3.24.3 and Camera package 0.11.0+2.

Future<void> switchCamera() async {
    if (cameraController != null) {
      final lensDirection = cameraController!.description.lensDirection;
      CameraDescription newDescription;
      if (lensDirection == CameraLensDirection.front) {
        newDescription = cameras.firstWhere((description) => description.lensDirection == CameraLensDirection.back);
      } else {
        newDescription = cameras.firstWhere((description) => description.lensDirection == CameraLensDirection.front);
      }
      await cameraController!.setDescription(newDescription);
    } else {
      await _initializeCameraController(_getDefaultCamera());
    }
  }

If you have encountered this error please let me know the solution.
Any help is always appreciated.

Upvotes: 0

Views: 63

Answers (1)

vijay
vijay

Reputation: 1

You must stop the video recording when switching the camera, then reinitialize the camera with the desired lens and start recording again.

The camera package does not yet support hot-switching cameras mid-recording without stopping and restarting.

Upvotes: -1

Related Questions