camera-samples: CameraX Back and front camera can't preview at same time ?

previewBack.setSurfaceProvider(viewBack.surfaceProvider)
previewFront.setSurfaceProvider(viewFront.surfaceProvider)
val cameraBack = cameraProvider.bindToLifecycle(this, cameraSelectorBack, previewBack)
val cameraFront = cameraProvider.bindToLifecycle(this, cameraSelectorFront, previewFront)
 java.lang.IllegalArgumentException: Multiple LifecycleCameras with use cases are registered to the same LifecycleOwner.
        at androidx.camera.lifecycle.LifecycleCameraRepository.bindToLifecycleCamera(LifecycleCameraRepository.java:269)
        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:423)
        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:274)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Hii guy’s use - cameraProvider.unbindAll() // Unbind use cases before rebinding,

btnSwitchCamera.setOnClickListener {

        if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA && cameraProvider.hasCamera(
                CameraSelector.DEFAULT_BACK_CAMERA)) {

            cameraProvider.unbindAll()    // Unbind use cases before rebinding

            cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
            val preview = Preview.Builder().build().also {
                it.setSurfaceProvider(viewFinder.createSurfaceProvider())
            }
            cameraProvider.bindToLifecycle(this, cameraSelector, preview)
            
        } else if (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA && cameraProvider.hasCamera(
                CameraSelector.DEFAULT_FRONT_CAMERA)) {

            cameraProvider.unbindAll()    // Unbind use cases before rebinding

            cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA
            val preview = Preview.Builder().build().also {
                it.setSurfaceProvider(viewFinder.createSurfaceProvider())
            }
            cameraProvider.bindToLifecycle(this, cameraSelector, preview)
        }

    }