Last active
July 31, 2022 18:27
-
-
Save mayowa-egbewunmi/e11cd2679b6d4cdf0f1a4b79a0e5db25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VideoCaptureManager ... LifecycleEventObserver { | |
... | |
fun showPreview(previewState: PreviewState, cameraPreview: PreviewView = getCameraPreview()): View { | |
getLifeCycleOwner().lifecycleScope.launchWhenResumed { | |
val cameraProvider = cameraProviderFuture.await() | |
cameraProvider.unbindAll() | |
//Select a camera lens | |
val cameraSelector: CameraSelector = CameraSelector.Builder() | |
.requireLensFacing(previewState.cameraLens) | |
.build() | |
//Create Preview use case | |
val preview: Preview = Preview.Builder() | |
.setTargetResolution(previewState.size) | |
.build() | |
.apply { setSurfaceProvider(cameraPreview.surfaceProvider) } | |
//Create Video Capture use case | |
val recorder = Recorder.Builder().build() | |
videoCapture = VideoCapture.withOutput(recorder) | |
cameraProvider.bindToLifecycle( | |
getLifeCycleOwner(), | |
cameraSelector, | |
preview, | |
videoCapture | |
).apply { | |
cameraControl.enableTorch(previewState.torchState == TorchState.ON) | |
} | |
} | |
return cameraPreview | |
} | |
fun updatePreview(previewState: PreviewState, previewView: View) { | |
showPreview(previewState, previewView as PreviewView) | |
} | |
private fun getCameraPreview() = PreviewView(getContext()).apply { | |
layoutParams = ViewGroup.LayoutParams( | |
ViewGroup.LayoutParams.MATCH_PARENT, | |
ViewGroup.LayoutParams.MATCH_PARENT | |
) | |
keepScreenOn = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment