-
Notifications
You must be signed in to change notification settings - Fork 414
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 DisconnectHandler Crash Android App #2195
Comments
Hi this is the repo to test. https://github.com/jhovelnu/MauiApp1/tree/MediaElement_Test However i couldnt reproduce this error in the test project. But i found something. In the Test project when i play the video: But when i play a video in my real project i see this: Could this be the cause of the error? This is the stack trace in app center: System.ObjectDisposedException Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self) |
That repository does not exist |
Sorry @ne0rrmatrix the repository was private, i have changed it to public already |
Your android manifest is missing some requirements. Can you look at this page: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/mediaelement?tabs=android and update the manifest with missing data. Please update the |
Hi, it's true that some things are missing in the MainActivity and the Manifest compared to the documentation. However, in the test project I shared, I haven't been able to replicate the error that does occur in my production application, and it happens when a new MainPage is set. In the test project, I tried to recreate the processes I perform in my production application, but the error does not replicate. Therefore, it is challenging to provide an explanation of what might be happening. Additionally, in my production project, I already tested adding the missing information to the Manifest and MainActivity, but the error persists. I will continue testing and investigating what might be causing the error, and if I find the cause, I will share it in this same thread. |
https://github.com/Pmr-precure/MediaElementDispose.git BR. |
Sorry for spamming your thread, but ive done some testing, and this is what i got: But then adding ForegroundService + delay seems to cause: In our app we use a foreground service to monitor some BLE devices. And videos are just tutorials. |
I looked at your sample. It is missing the required permissions and service in the android manifest. Please look at the instructions here: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/mediaelement?tabs=android |
I updated the samples and added the permissions + service. But its still the same. |
The dispose "bug" only seems happen when using App.Current.Mainpage = new page, going from 1 page with a MediaElement to a new page. Probably an issue in the MAUI core when setting the MainPage it will try to access the old one again? dunno. But setting the delay seems to do the job. |
@ne0rrmatrix The "bug" would be fixed if there was a try catch like ive done. But no idea if that would be fine. |
I'm currently migrating media element to media 3 library. If you can test against this PR: #2076 it would be appreciated. Any fix applied and merged to main would unlikely ever make it to a Nuget for current media 2 version. Any feedback on PR would be appreciated. Oh and if u remove the permissions for notifications it should just not start the service now. So if you want to disable notifications it will just disable service. |
Great! Ill test right away. "Oh and if u remove the permissions for notifications it should just not start the service now. So if you want to disable notifications it will just disable service" - But in our case, we already have a foreground permission since we use it for some BLE stuff, so we have no way of disabling this right? (Probably not a big deal anyways but.) |
@ne0rrmatrix Your PR did the trick with the ForegroundDidNotStartInTime exception! yay. It didnt fix the "Cannot access disposed object", but putting a delay before the disconnecthandler fixes that. Since then it wont be disposed before the removeView gets called. Thanks for your great work. |
If you create a discussion with a feature request to add the ability to disable notifications I will look at it. It is a feature I think would be a good idea. If you can get 10 up-votes in the next day or two I will start getting it ready and try and talk to the team about adding it. Up-voting features lets me push things through easier. |
Hello, hasn't this issue been resolved yet? In my case, even though a 'Delay' is configured, in some scenarios it doesn't work and the application closes. In my production application, there are more than 2000 user errors where the application crashes due to this issue. Is there any workaround other than using Delay? |
Can you check and see if this PR fixes your issue? #2076 If it does not can you get back to me. |
Hi, I’m experiencing crashes related to Environment:
Usage Details:
Permissions:
Additional Settings:
Expected Behavior:
Observed Behavior:
Steps to Reproduce:
Exception Stack Trace:
Could you advise if the permissions and ResizeableActivity setting are essential here (for local sounds only), or if there’s another approach to ensure stability with MediaElement in this scenario? Here's an example of my XAML (same in both views)
I then reference that in my ViewModel to play a sound. When the app is running as a background service the MAUI Kind Regards, Rob. |
I'm having a similar experience with MediaElement 4.1.2. In my case, AppShell has a TabBar instead of a ShellContent. The TabBar navigates to a home page, settings page and browse page. The browse page has some navigation to from a main group to a subgroup to an item detail page. If, after browsing to and playing a clip, the user selects the main tab or settings tab and then attempts to go back to the browse tab, I get cannot access a disposed object error. If I put the calls to mediaElement.Handler?.DisconnectHandler(); in either OnDisappearing() or Unloaded() I get System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'CommunityToolkit.Maui.Core.Views.MauiMediaElement'.' on that DicsonnectHandler() call. |
If you can test against this PR: #2076 and report if it solves your issue I would appreciate it. |
I had been trying many things over the last several days. Probably 20 hours experimenting and researching this one issue before I decided to post here. I was really worried that I was spending way too much time on something that was simple to do in the old Xamarin from 4 years ago, though at that time I was only targeting Android. After posting here I also discovered that MediaElement requires that it be part of the visual tree to work at all; even when only playing audio clips. I found this tidbit of information by removing the XAML MediaElement and adding the control and bindings in the page's class constructor. This idea was that I could manage the control's disposal manually and later move it to a Service class. However, if I commented out the Controls.Add(mediaElement); it would not play anything at all. Thus, eliminating the possibility of moving it to a Service class. I have no need for player controls or visual elements just to play sound effects. So, I started looking for some other way to do what I need. So, I just tried Plugin.Maui.Audio for the first time. It is working fine so far on Android. Tested for a couple of hours now with no issues. Still have to do some profiling and look for memory leaks, etc. I don't yet have an iOS device to play with yet either. If it turns out that this other package has issues with iOS or I have to go back to the drawing board for another reason, I'll return here. If after tomorrows testing my app is fine, then it means it's almost finished, and I could get it in the app stores before Xmas. I'm leery of trying to use an unreleased library version in an app I intend to publish. However, I will have to come back to this if the other package, which is working fine up to now, actually has other issues. Beyond that, I can possibly test this PR if I end up meeting my deadline and have some free time. I'm sorry I can't give you a better answer at this time, |
@mjmeans if you only need to play sound effects I would encourage you to use Plugin.Maui.Media. If there are issues with it then please feed them back on its repo. I help maintain that package too so we can work towards fixing things 😃 |
I did brieffly look at the PR. there is quite a lot of commits there. Too much to digest in a few minutes. One thing I noticed is that it's on version 8, which I think is a good thing. Greater compatibility with older devices and is on the long-term service branch, whereas .NET 9.0 is not. I'll look into your other repo in a few days to see what it can do. |
We are starting to see the Dispose Exception again, everytime we play a video, dispose it and then set the App.Mainpage, then we hit this. |
Hi, I am updating my MAUI App to Net9 and the Toolkit:MediaElement is crashing every time System.ObjectDisposedException: Cannot access a disposed object. Object name: 'CommunityToolkit.Maui.Core.Views.MauiMediaElement' even without playing a video. When I set the new MainPage the app crashes. |
The .NET MAUI team re-engineered Handler disconnection in .NET 9: https://learn.microsoft.com/en-us/dotnet/maui/whats-new/dotnet-9?view=net-maui-9.0#handler-disconnection If you are manually disconnecting the Handler, you need to first set |
Will try this tomorrow |
Hi, I added the HandlerProperties.SetDisconnectPolicy in the OnAppearing and now it works well, the app does not crash anymore. |
No crashes now :D weee. |
I might have been too quick to say it was crash free. We are now getting System.ObjectDisposedException: 'Cannot access a disposed object. Seems to happen in MediaControlsService.Android.cs 253 |
@Pmr-precure Could you do us a huge favor and open a new Issue for that bug? |
Hi, Ye i will. |
#2423 I added this issue, the repo replicates the Disposed object bug, and if a Delay is added before the disconnect it fixes it. Not sure whats going on |
Is there an existing issue for this?
Did you read the "Reporting a bug" section on Contributing file?
Current Behavior
Calling the DisconnectHandler of the mediaElement:
protected override async void OnDisappearing()
{
base.OnDisappearing();
}
With the Version 4.0.1 this code was working well, i had to add the Delay because the app crashes.
But with the last version 4.1.0 my app always crashing when i call the DisconnectHandler method, i have tried to remove the Delay and use the Unloaded event of the Content Page but the error still ocurrs.
Expected Behavior
The DisconnectHandler working well without app crash
Steps To Reproduce
1 - Add the Toolkit:MediaElement to a Page
2 - Configure any video file to reproduce
3 - Navigate to other page
4 - Call the DisconnectHandler in the page (Step 2)
5 - Then the app crash
Link to public reproduction project repository
https://github.com/jhovelnu/MauiApp1/tree/MediaElement_Test
Environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: