Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] MediaElement Crash Android #2423

Open
2 tasks done
Pmr-precure opened this issue Jan 3, 2025 · 16 comments
Open
2 tasks done

[BUG] MediaElement Crash Android #2423

Pmr-precure opened this issue Jan 3, 2025 · 16 comments
Labels
bug Something isn't working unverified

Comments

@Pmr-precure
Copy link

Pmr-precure commented Jan 3, 2025

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? #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

@Pmr-precure Pmr-precure added bug Something isn't working unverified labels Jan 3, 2025
Copy link
Contributor

Hi @Pmr-precure! We have added the needs reproduction label to this issue, which indicates that we are unable to take further action until you provide a reproduction sample that we can use to reproduce + fix your bug. In accordance with the Reporting a bug section of our Contributing.md that you confirmed reading before you submitted this Issue, we ask that every Issue author provide a link to an open-source repository containing the reproduction sample app. This issue will be closed automatically in 5 days if we do not receive a reproduction sample from you. Please feel free to re-open this Issue once you have provided a reproduction sample.

@Pmr-precure
Copy link
Author

Pmr-precure commented Jan 9, 2025

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

@cagriy
Copy link

cagriy commented Jan 13, 2025

I am having a very similar issue. I was disposing of the media element myself as well to avoid memory leaks in IOS; however, on Android, I get this very same crash regardless of whether I dispose of it or not.

When I leave the page where MediaElement lives, all seems fine, but if I call GC.Collect(), the application crashes with this exception.

EDIT: 4.1.2 on .NET 9 doesn't have this issue. Something in 5.0.0 could be introducing this problem.

@Pmr-precure
Copy link
Author

I am having a very similar issue. I was disposing of the media element myself as well to avoid memory leaks in IOS; however, on Android, I get this very same crash regardless of whether I dispose of it or not.

When I leave the page where MediaElement lives, all seems fine, but if I call GC.Collect(), the application crashes with this exception.

EDIT: 4.1.2 on .NET 9 doesn't have this issue. Something in 5.0.0 could be introducing this problem.

Yes i can confirm the crash doesnt happen on 4.1.2.

@ne0rrmatrix
Copy link
Contributor

Can you test the sample app with current main branch. The media 3 PR was just merged a few hours ago. It should solve most issues.

@Pmr-precure
Copy link
Author

Can you test the sample app with current main branch. The media 3 PR was just merged a few hours ago. It should solve most issues.

Hi, im currently trying to test it with the current main branch, but im getting this error:

Image

Are there anything i need to change or be aware of?

BR.

@ne0rrmatrix
Copy link
Contributor

Can you try using maui 9.0.30 with latest visual studio workload? I just tested the sample app and it builds and runs in both debug and release mode. I have seen the issue you are facing on a much older version of the android workload. Are you testing against main?

@Pmr-precure
Copy link
Author

Pmr-precure commented Jan 16, 2025

Can you try using maui 9.0.30 with latest visual studio workload? I just tested the sample app and it builds and runs in both debug and release mode. I have seen the issue you are facing on a much older version of the android workload. Are you testing against main?

Image

Image

Im testing against main yes.

Got a new error now

Image

@ne0rrmatrix
Copy link
Contributor

Based on the screenshots you provided can u update visual studio to latest to see if that fixes your issue?

@Pmr-precure
Copy link
Author

Based on the screenshots you provided can u update visual studio to latest to see if that fixes your issue?

I will try that when im back in office. Ill let you know.

Br

@Pmr-precure
Copy link
Author

Based on the screenshots you provided can u update visual studio to latest to see if that fixes your issue?

Image
Still getting this after updating VS to 17.12.4

Image

@ne0rrmatrix
Copy link
Contributor

I looked at the sample and it does not have the correct permissions for android manifest. You are missing the service.

@Pmr-precure
Copy link
Author

Pmr-precure commented Jan 16, 2025

I looked at the sample and it does not have the correct permissions for android manifest. You are missing the service.

Added that, didnt work.

But set 21.0 to 26, then it wont crash. then it started crashing on the permission which i now added :D

Image

@ne0rrmatrix
Copy link
Contributor

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

@Pmr-precure
Copy link
Author

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

Roger that, will test now. And get back to you.

I still think its odd that you need to add the permisson to use the MediaElement, since we dont want to use the foreground service anyways, we only show some tutorials etc.

@Pmr-precure
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unverified
Projects
None yet
Development

No branches or pull requests

4 participants