diff --git a/app/src/main/java/com/haishinkit/app/CameraScreen.kt b/app/src/main/java/com/haishinkit/app/CameraScreen.kt index 7d105f7c..c6d161a7 100644 --- a/app/src/main/java/com/haishinkit/app/CameraScreen.kt +++ b/app/src/main/java/com/haishinkit/app/CameraScreen.kt @@ -136,19 +136,40 @@ fun CameraScreen( }, onVideoPermissionStatus = { state -> when (state.status) { PermissionStatus.Granted -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - stream.attachVideo(MultiCamera2Source(context)) - (stream.videoSource as? MultiCamera2Source)?.apply { - open(0, CameraCharacteristics.LENS_FACING_BACK) - open(1, CameraCharacteristics.LENS_FACING_FRONT) - getVideoByChannel(1)?.apply { - frame = Rect(20, 20, 90 + 20, 160 + 20) + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + stream.attachVideo(MultiCamera2Source(context)) + (stream.videoSource as? MultiCamera2Source)?.apply { + try { + open(0, CameraCharacteristics.LENS_FACING_BACK) + open(1, CameraCharacteristics.LENS_FACING_FRONT) + getVideoByChannel(1)?.apply { + frame = Rect(20, 20, 90 + 20, 160 + 20) + } + } catch (e: Exception) { + Log.e(TAG, "Error while setting up multi-camera: ${e.message}", e) + // If multi-camera setup fails, revert to single camera + stream.attachVideo(null) + stream.attachVideo(Camera2Source(context).apply { + try { + open(CameraCharacteristics.LENS_FACING_BACK) + } catch (e: Exception) { + Log.e(TAG, "Error while opening main camera: ${e.message}", e) + } + }) + } } + } else { + stream.attachVideo(Camera2Source(context).apply { + try { + open(CameraCharacteristics.LENS_FACING_BACK) + } catch (e: Exception) { + Log.e(TAG, "Error while opening camera: ${e.message}", e) + } + }) } - } else { - (stream.videoSource as? Camera2Source)?.apply { - open(CameraCharacteristics.LENS_FACING_BACK) - } + } catch (e: Exception) { + Log.e(TAG, "General error while setting up camera: ${e.message}", e) } }