CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.28k stars 404 forks source link

[BUG] MediaElement Android .NET8 crashing #2143

Closed KasperSommer closed 3 months ago

KasperSommer commented 3 months ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

I have created a new Maui project. Added Community Toolkit and Community Toolkit MediaElement (following https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/mediaelement?tabs=android#clean-up-mediaelement-resources)

Created a new page (Test.xaml) with the mediaElement. When the media element is started or stopped the following error is causing an exception:

MediaManager: Error: AndroidX.LocalBroadcastManager.Content.LocalBroadcastManager not supported on Android 13 and above.

Added clean up from same link above:

...
Unloaded="ContentPage_Unloaded">
  void ContentPage_Unloaded(object? sender, EventArgs e)
  {
      // Stop and cleanup MediaElement when we navigate away
      mediaElement.Handler?.DisconnectHandler();
  }

This causes the entire app to crash with exception:

Information: 0 : 
MediaControlsService destroyed.
The program 'xxxxx.MAUI.dll' has exited with code 0 (0x0).

Expected Behavior

Well, just being able to play and stop video and also leaving the page without the app to cause exceptions and crash.

Steps To Reproduce

  1. Open and run solution.
  2. Click button AAA.
  3. Start/stop and/or press back button.

Link to public reproduction project repository

https://github.com/KasperSommer/MediaElementIssue

Environment

- .NET MAUI CommunityToolkit: 9.0.3
- .NET MAUI CommunityToolkit.Maui.MediaElement: 4.1.0
- OS: Android 34
- .NET MAUI: net8.0-android

Anything else?

I have mentioned it here as well: https://stackoverflow.com/questions/78894331/maui-community-toolkit-mediaelement-crashing?noredirect=1#comment139101057_78894331

I really hope I have done this correctly :)

ne0rrmatrix commented 3 months ago

The log message about broadcast manager can be ignored. As to the crash I will look into that today. For now a workaround to keep testing is remove the handler disconnect for now. I should have a fix today or tomorrow to test.

KasperSommer commented 3 months ago

Thank you very much, sounds good. I can remove the disconnect in the above simple test, however, I also use toolkit IPopupService in my real app which results in the same crash.

bijington commented 3 months ago

Can you also call disconnect in the Closing of a Popup?

KasperSommer commented 3 months ago

I am sorry, it might not be due to the popup. It might actually be just by going to and back from the page without the disconnect.

ne0rrmatrix commented 3 months ago

Ok this is a big issue. Easy workaround though. Add an image as Artwork will prevent crash. Fix will be out later today!

Example workaround:

 <toolkit:MediaElement 
WidthRequest="500" 
HeightRequest="400" 
MetadataArtworkUrl = "https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm" 
Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" />
ne0rrmatrix commented 3 months ago

@KasperSommer can you test this PR against your project https://github.com/CommunityToolkit/Maui/pull/2144 It should be fixed now in this PR.

ne0rrmatrix commented 3 months ago

I have fixed the right bug this time I think? The previous listed bug was for a CTD in windows. Sorry about the confusion. The issue listed above was app exiting and not actually crashing. The destroy method was being called on page exit and it had a command to exit app. That was a poor decision by me to put that their. I have removed all of that. Now it will only close the service and not shut the app down.

KasperSommer commented 3 months ago

Wow, thank you for very quick action, amazing 😊 It seems to work perfectly in my project now, also with/without popups. Do you have any idea when it will be available on Nuget?

Thanks again!

GreenleafDk commented 2 months ago

AndroidX.LocalBroadcastManager.Content.LocalBroadcastManager not supported on Android 13 and above] While this does not cause a crash, it does spam the log, and it would be nice if it went away. After testing for half an hour, I now have 200+ occurrences of this in the log.

ne0rrmatrix commented 2 months ago

AndroidX.LocalBroadcastManager.Content.LocalBroadcastManager not supported on Android 13 and above] While this does not cause a crash, it does spam the log, and it would be nice if it went away. After testing for half an hour, I now have 200+ occurrences of this in the log.

Latest version on Main has those log messages removed. It will be out on next nuget.

KasperSommer commented 2 months ago

@ne0rrmatrix do you know when next nuget will be? Our project is stuck on this exit on mediaelement error and we cannot move ahead. It was fixed 3 weeks ago. Thanks.