Skip to content

Commit

Permalink
Improve camera source error handling and fallback mechanism
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
8thgencore committed Feb 27, 2025
1 parent 454c808 commit b9cedda
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions app/src/main/java/com/haishinkit/app/CameraScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit b9cedda

Please sign in to comment.