Description
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.cs
currently has noMediaCaptureFailed
event handler, but when it would have one, you could observe the exception, with theFailureReason
text: "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
UnhandledException
handler.
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()