fix: Close CameraSession
if the View is removed (#2174)
* fix: Close `CameraSession` if the View is removed * fix: Use ViewManager's `onDropViewInstance` instead * fix: Only stop repeating if we have a session * fix: Reset `configuration` on `close()`
This commit is contained in:
parent
98a641702c
commit
a7e706150e
@ -131,6 +131,10 @@ class CameraView(context: Context) :
|
||||
super.onDetachedFromWindow()
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
cameraSession.close()
|
||||
}
|
||||
|
||||
fun update() {
|
||||
Log.i(TAG, "Updating CameraSession...")
|
||||
|
||||
|
@ -31,6 +31,11 @@ class CameraViewManager : ViewGroupManager<CameraView>() {
|
||||
|
||||
override fun getName(): String = TAG
|
||||
|
||||
override fun onDropViewInstance(view: CameraView) {
|
||||
view.destroy()
|
||||
super.onDropViewInstance(view)
|
||||
}
|
||||
|
||||
@ReactProp(name = "cameraId")
|
||||
fun setCameraId(view: CameraView, cameraId: String) {
|
||||
view.cameraId = cameraId
|
||||
|
@ -172,6 +172,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
|
||||
codeScannerOutput?.close()
|
||||
codeScannerOutput = null
|
||||
|
||||
configuration = null
|
||||
isRunning = false
|
||||
}
|
||||
|
||||
@ -363,16 +364,16 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
|
||||
}
|
||||
|
||||
private fun configureCaptureRequest(config: CameraConfiguration) {
|
||||
val device = cameraDevice ?: throw NoCameraDeviceError()
|
||||
val captureSession = captureSession ?: throw CameraNotReadyError()
|
||||
|
||||
if (!config.isActive) {
|
||||
// TODO: Do we want to do stopRepeating() or entirely destroy the session?
|
||||
// If the Camera is not active, we don't do anything.
|
||||
captureSession.stopRepeating()
|
||||
captureSession?.stopRepeating()
|
||||
return
|
||||
}
|
||||
|
||||
val device = cameraDevice ?: throw NoCameraDeviceError()
|
||||
val captureSession = captureSession ?: throw CameraNotReadyError()
|
||||
|
||||
val previewOutput = previewOutput
|
||||
if (previewOutput == null) {
|
||||
Log.w(TAG, "Preview Output is null, aborting...")
|
||||
|
Loading…
Reference in New Issue
Block a user