Maui: [BUG] MediaElement Crash Android

Is there an existing issue for this?

  • I have searched the existing issues

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

Current Behavior

Hi,

I have a weird issue, I dispose my mediaelements manually because of other issues.

But now im starting to get this crash: Image

This is the dispose method of the MediaControlService.Android. It doesnt get called by the DisconnectHandler method, but seems to get randomly called. Since it seems to happen random i havnt been able to replicate. But it causes a lot of crashes in our app.

** EDIT**

I finally managed to replicate the issue, and it seems to be because of the Garbage Collector.

Our App is using Bluetooth and doing a lot of data processing, so the GC is working hard. And at somepoint the GC apparently “kills” the MediaSession which leads to this crash ( Eventhough the media service should have been disposed already) . In this repo https://github.com/Pmr-precure/MauiTestProject/tree/Audio_And_Session_Dispose_Bug i added a “MemoryFiller” so after starting the video by pressing “Click me”, then go back to the homepage and start the memory filler. The crash will happen seconds later.

I think this issue could be the same? https://github.com/CommunityToolkit/Maui/issues/2359

Expected Behavior

Not randomly crash.

Steps To Reproduce

Git clone Run project Press the button, and see it crash.

Link to public reproduction project repository

Git repo

Environment

- .NET MAUI CommunityToolkit:10.0.0
- OS: Android
- .NET MAUI: 9.0.21

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23 (10 by maintainers)

Most upvoted comments

I checked the sample app and saw a few issues that would affect the ability to test this issue. I fixed them. I switched to using standard navigation pattern and removed the state container for community toolkit. Was it there for a specific purpose? The bindings for the state container were also not configured for dotnet 9.x. After making these changes to navigation to do what I think you intended the disconnect worked as intended. BTW with dotnet 9 handler disconnection is now automatic and unless you have a specific need to handle it yourself Maui can handle that for you. It is a breaking change from dotnet 8.

My assumption is that you want to disconnect handler manually upon leaving the page. You want to create a new page each time you navigate? If so that has been tested with shell navigation and found to be working. I used your sample and modified the navigation to follow standard patterns.

If the issue persists please open a new bug report. I am closing this now as resolved.

https://github.com/CommunityToolkit/Maui/releases/tag/10.0.0 is where u can find the list of breaking changes to the Maui community toolkit

I can confirm that the current main branch with the media3 update fixes this issue! Hurray

It doesnt fix this samples other “Already disposed” bug. https://github.com/Pmr-precure/MauiTestProject/tree/master But that can be fixed adding a delay before manually disposing it.

Thank you very much @ne0rrmatrix

BR

The minimum supported version of android for media element is API level 26 for dotnet 9.x going forward

Only way i have been able to fix this issue is to add GC.SupressFinalize(this) in the dispose method of the MediaControlService. And then call dispose(true) from the onDestroy() method. No idea if this is the fix or not.

Or simply just remove the mediaSession.Release() and Dispose since it seems like its already disposed from somewhere else. 😃

Image

Image