Skip to content

bug: switching lensFacing when camera is already running generates a black screen on iOS sometimes #335

@lajuffermans

Description

@lajuffermans

Plugin(s)

  • Barcode Scanning
  • Face Detection
  • Face Mesh Detection
  • Selfie Segmentation
  • Subject Segmentation
  • Translation

Version

8.1.0

Platform(s)

  • Android
  • iOS
  • Web

Current behavior

On iOS switching lensFacing between FRONT and BACK sometimes gives a black screen (and the camera doesn't start).

Expected behavior

Switch between FRONT and BACK camera without issues.

Reproduction

https://github.com/lajuffermans/capacitor-mlkit-plugin-demo

Steps to reproduce

  1. Clone the repo
  2. Build and run it
  3. In the demo app go to "Barcode Scanning"
  4. Select "Start scan" (make sure you requested camera permissions)
  5. In the bottom left corner there is a button to toggle between front and back camera, use it a couple of times and you get a black screen.

Other information

  • Only noticed this on iOS
  • At first I tried to solve this by manually use stopScan and then startScan with the new lensFacing valu however when I looked at the code I noticed startScan already calls stopScan
  • I'm not an iOS expert so I asked Claude (yes here we go) and Claude suggested to add a self.cameraView?.stopCaptureSession() in the stopScan of BarcodeScanner and it added this to BarcodeScannerView:
    // Breaks the AVCaptureVideoDataOutput→delegate retain cycle and stops the session.
    // Must be called explicitly before releasing the view; deinit cannot be relied upon
    // because setSampleBufferDelegate holds a strong reference, preventing deallocation.
    public func stopCaptureSession() {
        captureSession?.outputs.forEach {
            ($0 as? AVCaptureVideoDataOutput)?.setSampleBufferDelegate(nil, queue: nil)
        }
        captureSession?.stopRunning()
    }

I have to say, that does work for me but I'd rather have an expert look at it ;)

Capacitor doctor

💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 8.3.4
@capacitor/core: 8.3.4
@capacitor/android: 8.3.4
@capacitor/ios: 8.3.4

Installed Dependencies:

@capacitor/cli: 8.3.4
@capacitor/core: 8.3.4
@capacitor/android: 8.3.4
@capacitor/ios: 8.3.4

[success] iOS looking great! 👌
[success] Android looking great! 👌

Before submitting

  • I have read and followed the bug report guidelines.
  • I have attached links to possibly related issues and discussions.
  • I understand that incomplete issues (e.g. without reproduction) are closed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions