Browse Source

ImageViewer, Android camera (#3487)

* The camera should stop working after leaving CameraView on Android.
* Update examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt
pull/3567/head
Rob X 1 year ago committed by GitHub
parent
commit
494c686293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

13
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

@ -79,6 +79,7 @@ private fun CameraWithGrantedPermission(
val context = LocalContext.current val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current val lifecycleOwner = LocalLifecycleOwner.current
val viewScope = rememberCoroutineScope() val viewScope = rememberCoroutineScope()
var cameraProvider: ProcessCameraProvider? by remember { mutableStateOf(null) }
val preview = Preview.Builder().build() val preview = Preview.Builder().build()
val previewView = remember { PreviewView(context) } val previewView = remember { PreviewView(context) }
@ -96,16 +97,22 @@ private fun CameraWithGrantedPermission(
.build() .build()
} }
DisposableEffect(Unit) {
onDispose {
cameraProvider?.unbindAll()
}
}
LaunchedEffect(isFrontCamera) { LaunchedEffect(isFrontCamera) {
val cameraProvider = suspendCoroutine<ProcessCameraProvider> { continuation -> cameraProvider = suspendCoroutine<ProcessCameraProvider> { continuation ->
ProcessCameraProvider.getInstance(context).also { cameraProvider -> ProcessCameraProvider.getInstance(context).also { cameraProvider ->
cameraProvider.addListener({ cameraProvider.addListener({
continuation.resume(cameraProvider.get()) continuation.resume(cameraProvider.get())
}, executor) }, executor)
} }
} }
cameraProvider.unbindAll() cameraProvider?.unbindAll()
cameraProvider.bindToLifecycle( cameraProvider?.bindToLifecycle(
lifecycleOwner, lifecycleOwner,
cameraSelector, cameraSelector,
preview, preview,

Loading…
Cancel
Save