Skip to content

Commit b9cedda

Browse files
committed
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
1 parent 454c808 commit b9cedda

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

app/src/main/java/com/haishinkit/app/CameraScreen.kt

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,40 @@ fun CameraScreen(
136136
}, onVideoPermissionStatus = { state ->
137137
when (state.status) {
138138
PermissionStatus.Granted -> {
139-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
140-
stream.attachVideo(MultiCamera2Source(context))
141-
(stream.videoSource as? MultiCamera2Source)?.apply {
142-
open(0, CameraCharacteristics.LENS_FACING_BACK)
143-
open(1, CameraCharacteristics.LENS_FACING_FRONT)
144-
getVideoByChannel(1)?.apply {
145-
frame = Rect(20, 20, 90 + 20, 160 + 20)
139+
try {
140+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
141+
stream.attachVideo(MultiCamera2Source(context))
142+
(stream.videoSource as? MultiCamera2Source)?.apply {
143+
try {
144+
open(0, CameraCharacteristics.LENS_FACING_BACK)
145+
open(1, CameraCharacteristics.LENS_FACING_FRONT)
146+
getVideoByChannel(1)?.apply {
147+
frame = Rect(20, 20, 90 + 20, 160 + 20)
148+
}
149+
} catch (e: Exception) {
150+
Log.e(TAG, "Error while setting up multi-camera: ${e.message}", e)
151+
// If multi-camera setup fails, revert to single camera
152+
stream.attachVideo(null)
153+
stream.attachVideo(Camera2Source(context).apply {
154+
try {
155+
open(CameraCharacteristics.LENS_FACING_BACK)
156+
} catch (e: Exception) {
157+
Log.e(TAG, "Error while opening main camera: ${e.message}", e)
158+
}
159+
})
160+
}
146161
}
162+
} else {
163+
stream.attachVideo(Camera2Source(context).apply {
164+
try {
165+
open(CameraCharacteristics.LENS_FACING_BACK)
166+
} catch (e: Exception) {
167+
Log.e(TAG, "Error while opening camera: ${e.message}", e)
168+
}
169+
})
147170
}
148-
} else {
149-
(stream.videoSource as? Camera2Source)?.apply {
150-
open(CameraCharacteristics.LENS_FACING_BACK)
151-
}
171+
} catch (e: Exception) {
172+
Log.e(TAG, "General error while setting up camera: ${e.message}", e)
152173
}
153174
}
154175

0 commit comments

Comments
 (0)