Maui: [BUG] CameraView on Windows crashes app when the camera is in use
Is there an existing issue for this?
- I have searched the existing issues
Did you read the “Reporting a bug” section on Contributing file?
- I have read the “Reporting a bug” section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
The CommunityToolkit.Maui sample app (as well as my internally developed .NET MAUI app) crashes when the camera is already in use. The CameraView control sends a MediaCapturedFailed event but crashes anyway, as the exception reported is rethrown, with no possibility to handle it.
Expected Behavior
The CameraView sends a MediaCapturedFailed event when the camera is already in use, but doesn’t crash the app.
Steps To Reproduce
- Start the CommunityToolkit.Maui sample app on Windows and navigate to the “Views > CameraView Page”. Observe the live camera preview.
- Press the “StopCameraPreview” button
- Start the Camera app that comes with Windows and adjust the selected camera to be the same as used in 1.
- Press the “StartCameraPreview” button
- Observe a black preview image
- Press the “Capture Image” button
- (the
CameraViewPage.xaml.cscurrently has noMediaCaptureFailedevent handler, but when it would have one, you could observe the exception, with theFailureReasontext: “Hardware MFT failed to start streaming due to lack of hardware resources.” - The app crashes; when the debugger is attached, it breaks in the .NET MAUI Windows app’s standard
UnhandledExceptionhandler.
Alternative steps to reproduce would be: 9. Start the Camera app that comes with Windows 10. Start the CommunityToolkit.Maui sample app on Windows and navigate to the “Views > CameraView Page”. Observe a gray area where the live camera preview would be. 11. Continue with 4., like above
I also tested using Android, and it’s a bit harder to reproduce the error, but it at least doesn’t crash the app. Basically if you put the CommunityToolkit.Maui sample app in split mode with the Android devices’ camera app, you can let the camera app “steal” the camera preview from the CommunityToolkit.Maui app and still press “Capture Image”. The MediaCaptureFailed event handler is called again, but the exception isn’t rethrown. Inspecting the code where ICameraView.OnMediaCapturedFailed() is called suggests that the error only occurs on Windows.
Link to public reproduction project repository
https://github.com/CommunityToolkit/Maui
Environment
- .NET MAUI CommunityToolkit: 11.1.0 (I used the Git repository directly, with my local main branch set to the "11.1.0" tag
- OS: Windows 11 (Version 22H2 (Build 22621.4317))
- .NET MAUI: The CommunityToolkit.Maui.Camera project references 9.0.30, the sample project references "*", which for Visual Studio 17.13.0 is already version 9.0.40.
Anything else?
The stack trace in the sample app looks like:
CommunityToolkit.Maui.Camera.dll!CommunityToolkit.Maui.Core.CameraManager.<PlatformTakePicture>d__20.MoveNext() Line 109 C#
[Resuming Async Method]
[External Code]
[Async Call Stack]
[Async] CommunityToolkit.Maui.Camera.dll!CommunityToolkit.Maui.Core.Handlers.CameraViewHandler.MapCaptureImage(CommunityToolkit.Maui.Core.Handlers.CameraViewHandler handler = {CommunityToolkit.Maui.Core.Handlers.CameraViewHandler}, CommunityToolkit.Maui.Core.ICameraView view = CommunityToolkit.Maui.Views.CameraView: BindingContext = CommunityToolkit.Maui.Sample.ViewModels.Views.CameraViewViewModel, Bounds = {X=0 Y=0 Width=1080 Height=574}, object arg3 = null) Line 141 C#
The stack trace in my app is:
at CommunityToolkit.Maui.Core.CameraManager.<PlatformTakePicture>d__20.MoveNext()
at CommunityToolkit.Maui.Core.Handlers.CameraViewHandler.<MapCaptureImage>d__12.MoveNext()
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (6 by maintainers)
It’s also worth noting that developers don’t have to subscribe to events in code behind. We provide the
EventToCommandBehaviorto allow them to bind the event into their view modelThere is already the
CameraManagerthat handles the business logic. I believe the CameraView API was built the way it is to reduce the amount of setup a developer needs to do. I suspect theCameraManagercould easily be exposed to break that boundary if we wanted to