From b9ceddae7b4a16387d1d55f47b86de8f7ca0d0e7 Mon Sep 17 00:00:00 2001 From: 8thgencore Date: Thu, 27 Feb 2025 18:21:30 +0300 Subject: [PATCH] Improve camera source error handling and fallback mechanism - Add comprehensive error handling for multi-camera and single camera setups - Implement fallback to single camera source if multi-camera initialization fails - Log detailed error messages for different camera setup scenarios - Ensure graceful handling of camera permission and initialization errors --- .../java/com/haishinkit/app/CameraScreen.kt | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) 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) } }