CameraView fix for Android: video recording and rotation#3168
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Android CameraManager implementation to improve repeated video recording reliability and to apply device rotation consistently across CameraX use cases (preview, image capture, and video capture).
Changes:
- Keeps
VideoCapture/Recorderalive across recording sessions by stopping disposal inCleanupVideoRecordingResources(). - Rebuilds video recording use cases on-demand in
PlatformStartVideoRecording()when they are missing (e.g., after a resolution change). - Centralizes orientation-to-surface-rotation mapping and applies it to
imageCapture,videoCapture, andcameraPreview.
…ideo capture session
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| .SetCaptureMode(imageCaptureMode) | ||
| .SetResolutionSelector(resolutionSelector) | ||
| ?.Build() ?? throw new InvalidOperationException("Unable to set resolution selector"); | ||
| .Build(); |
There was a problem hiding this comment.
Rebuilding imageCapture here creates a brand-new ImageCapture instance, which will reset any previously applied configuration (e.g., flash mode set via UpdateFlashMode). Consider re-applying the current cameraView.CameraFlashMode (and any other relevant settings) to the new imageCapture immediately after building it, so starting/stopping video recording or session rebinds don’t silently revert user settings.
| .Build(); | |
| .Build(); | |
| if (cameraView is not null) | |
| { | |
| UpdateFlashMode(cameraView.CameraFlashMode); | |
| } |
|
|
||
| await mediaRecording.StopAsync(); | ||
|
|
||
| if(videoCaptureStream.CanSeek) |
There was a problem hiding this comment.
Code style: add a space after if to match the surrounding formatting (if (videoCaptureStream.CanSeek)).
| if(videoCaptureStream.CanSeek) | |
| if (videoCaptureStream.CanSeek) |
TheCodeTraveler
left a comment
There was a problem hiding this comment.
Thanks @zhitaop!!
…olkit#3168) * Fix video recording and use case target rotation * Address Copilot review * Refactor building and binding use cases, separate image capture and video capture session * Update CameraViewPage MediaCaptured handler * Refactor `CaptureSessionMode` enum * Update src/CommunityToolkit.Maui.Camera/CameraManager.android.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Set `videoRecordingStream.Position = 0` before returning `videoRecordingStream` * Fix CameraView page on Windows Sample App * Remove the need to update Stream Position in Sample App --------- Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Description of Change
This PR fixes a bug on Android where attempting to record a video for a second time will fail:
CleanupVideoRecordingResources()no longer disposesvideoRecorderandvideoCaptureafter each recording session. This prevents the "Null Stream" error caused by trying to record with a disposed object.Added a
CaptureSessionModeenum and session state tracking to distinguish image capture and video capture workflows.Refactored camera setup into smaller use case rebuild/rebind steps
Image capture workflow no longer binds the
videoCaptureuse case as it might slow down image capture. Only bindsvideoCaptureuse case when recording starts.Improves orientation handling by applying rotation consistently to
imageCapture,videoCapture, andcameraPreview.Updated
OnMediaCaptured()in the sample app to avoid file locking issues and improves image displayLinked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information