Skip to content

CameraView fix for Android: video recording and rotation#3168

Merged
TheCodeTraveler merged 15 commits intoCommunityToolkit:mainfrom
zhitaop:fix/camera-videorecord-and-rotation-android
Apr 2, 2026
Merged

CameraView fix for Android: video recording and rotation#3168
TheCodeTraveler merged 15 commits intoCommunityToolkit:mainfrom
zhitaop:fix/camera-videorecord-and-rotation-android

Conversation

@zhitaop
Copy link
Copy Markdown
Contributor

@zhitaop zhitaop commented Mar 20, 2026

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 disposes videoRecorder and videoCapture after each recording session. This prevents the "Null Stream" error caused by trying to record with a disposed object.

  • Added a CaptureSessionMode enum 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 videoCapture use case as it might slow down image capture. Only binds videoCapture use case when recording starts.

  • Improves orientation handling by applying rotation consistently to imageCapture, videoCapture, and cameraPreview.

  • Updated OnMediaCaptured() in the sample app to avoid file locking issues and improves image display

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

Copilot AI review requested due to automatic review settings March 20, 2026 09:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/Recorder alive across recording sessions by stopping disposal in CleanupVideoRecordingResources().
  • 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, and cameraPreview.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

.SetCaptureMode(imageCaptureMode)
.SetResolutionSelector(resolutionSelector)
?.Build() ?? throw new InvalidOperationException("Unable to set resolution selector");
.Build();
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
.Build();
.Build();
if (cameraView is not null)
{
UpdateFlashMode(cameraView.CameraFlashMode);
}

Copilot uses AI. Check for mistakes.

await mediaRecording.StopAsync();

if(videoCaptureStream.CanSeek)
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style: add a space after if to match the surrounding formatting (if (videoCaptureStream.CanSeek)).

Suggested change
if(videoCaptureStream.CanSeek)
if (videoCaptureStream.CanSeek)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zhitaop!!

@TheCodeTraveler TheCodeTraveler merged commit 87e176c into CommunityToolkit:main Apr 2, 2026
10 checks passed
matt-goldman pushed a commit to matt-goldman/Maui-1 that referenced this pull request Apr 2, 2026
…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>
@zhitaop zhitaop deleted the fix/camera-videorecord-and-rotation-android branch April 3, 2026 02:06
@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

3 participants