-
Notifications
You must be signed in to change notification settings - Fork 348
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] Crash when cleaning up MediaElement on Windows #962
Comments
I was creating an issue to report the same problem. This exception also occurred by running CommunityToolkit.Maui.Sample project: after MediaElement Page was displayed and clicking on "back" buttuon, the By looking the stack trace
it seems that exception was thrown when accessing the property To be excact: // method of MediaManager.windows.cs
protected override void DisconnectHandler(MauiMediaElement platformView)
{
platformView.Dispose(); // <- MediaPlayer is disposed here
Dispose(); // <- access to property MediaPlayer.PlaybackSession exception occurred
base.DisconnectHandler(platformView);
}
// method of MauiMediaElement
public void Dispose()
{
mediaElement?.MediaPlayer.Dispose();
}
// method of MediaManager.windows.cs
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (Player?.MediaPlayer is not null)
{
if (displayActiveRequested)
{
DisplayRequest.RequestRelease();
displayActiveRequested = false;
}
Player.MediaPlayer.MediaOpened -= OnMediaElementMediaOpened;
Player.MediaPlayer.MediaFailed -= OnMediaElementMediaFailed;
Player.MediaPlayer.MediaEnded -= OnMediaElementMediaEnded;
Player.MediaPlayer.VolumeChanged -= OnMediaElementVolumeChanged;
if (Player.MediaPlayer.PlaybackSession is not null) // <- exception was thrown here
{
Player.MediaPlayer.PlaybackSession.PlaybackRateChanged -= OnPlaybackSessionPlaybackRateChanged;
Player.MediaPlayer.PlaybackSession.PlaybackStateChanged -= OnPlaybackSessionPlaybackStateChanged;
Player.MediaPlayer.PlaybackSession.SeekCompleted -= OnPlaybackSessionSeekCompleted;
}
}
}
} Changing the method DisconnectHandler by calling platformView.Dispose after MediaManager.Dispose, seems to solve the problem, but I can't figure out what this simple change can really involve // method of MediaManager.windows.cs with method invocation switched
protected override void DisconnectHandler(MauiMediaElement platformView)
{
//platformView.Dispose();
Dispose();
platformView.Dispose();
base.DisconnectHandler(platformView);
} I hope this information can help community to solve the problem |
Crashes on android too |
I had same issue till I did a null check for sender like this. inside xaml:
inside xaml.page.cs
Event args goes off fairly often and you don't want it to trigger accidently. Try adding above null check and see if that helps. I had exact same issue and this fixed it for me. This is example and the relavent code in xaml is:
|
@ne0rrmatrix I've tried your solution, but I still have the same problem. I changed the void BasePage_Unloaded(object? sender, EventArgs e)
{
if (sender is null)
{
return;
}
// Stop and cleanup MediaElement when we navigate away
mediaElement.Handler?.DisconnectHandler();
} but unfortunately in my tests the sender argument is never null and
Tested everything with the current state of main branch of this repo |
I was able to replicate issue by adding a debug print statement before handler. I think the handler was not being invoked until I added code to the unload event.
If I copy paste the code I put earlier in comments I think it simply never fires, at least on Windows. If I had a simple debug statement it fires and app crashes just like you described. I have no idea what is going on. I have been debugging an issue with media element that might be related. But I have to put together a sample and verify the specific cause. I am going to spend the next day or two working on this. I have to find a work around for my existing project. Right now I am banging my head against the proverbial desk trying to figure out what is wrong. I have a program that plays podcasts. I added a feature to track playback across a list of shows. Setting the page with media element as transient causes issues. If I have the page with media element on it set as singleton in MauiProgram.cs I have issues with my code not firing when I switch shows. But If I enable it as singleton media element works flawlessly except for the above crash If I enable unload event. So it works fine if I don't use the code I developed to track shows. So tracking down a related issue the last few days. I don't know why media element does not want to play nice with MVVM AddTransient. It crashes at third podcast and fails to load show. No errors in debug console. Just fails to load show. It appears to try and load but never completes. Changing to singleton removes issue but I will have to rework code to deal with that. Might as well try and isolate the issue by creating a separate project and validate the specific problem. That might give me a work around for my needs that can be used by the wider community. I will try and isolate it by creating a project that is bare bones with only code necessary to validate the issue. I am almost done this starter project for the podcasting app. It has been fun and learning experience. I wanted to try out something new and learn at the same time. I am loving Maui but it can be hard to verify the issue I have are not user errors and genuine bugs. Taking the time to validate a repeatable flaw in a sample projects has let me not fill up the bug reports with stuff where I have found out later was user error. That has happened about a half dozen times already. |
How do I implement this work around? I would like to test it. I have submitted a related bug. If this workaround fixes my issue I can delete my bug report. I am using add transient and it fails to load a third video every time, and it only affects windows. Works fine on Android and iOS. So implementing workaround to see if this bug is the same for me will let me delete my report if it works. |
Could you maybe try the resulting NuGet from #1002? That should have this fix. |
I tried it. It appears to properly dispose of MediaElement. I get no errors. Does not solve my issue of after playing two video going and trying to play a third still fails to open. It tries and platform shows video controls but stream never opens. I have example in my bug report of how to show bug and sample is provided. I have tested the fix and can verify no errors on disconnecting handler. But I still cant play three video's in a row. My issue only affects Windows. It is still unresolved. But the bug with media element handler disconnect is fixed from this PR. |
Is this https://pkgs.dev.azure.com/dotnet/696bc9fd-f160-4e97-a1bd-7cbbb3b58f66/_packaging/bdfb41c2-3c30-78f0-b915-ea20f2a39909/nuget/v2/ the url of package source from which i can donwload the resulting Nuget of #1002 ? |
I don't think so. See the instructions and correct URL here: https://github.com/CommunityToolkit/Maui/wiki/Preview-Packages#pull-requests- |
Thanks @jfversluis. |
Tested with version 1.0.0-build-1002.87588 and DiconnectHander doesn't throw exception anymore. |
Perfect, thanks! |
Hello guys, I have the latest .net 7 update and the latest communiltytoolkit and mediaelements installed (currently 5.1.0 and 1.0.2) and this issue is there. As soon as i dispose the handler and it tries to go to the previous view or any other i get the crash (unhandled expection). any ideas? |
@zisi911 please open a new issue and provide a small sample that reproduces the issue |
Is there an existing issue for this?
Did you read the "Reporting a bug" section on Contributing file?
Current Behavior
Expected Behavior
App navigates back and resources are cleaned up
Steps To Reproduce
App navigates back and a crash occurs
Link to public reproduction project repository
https://github.com/wldevries/MauiMediaElementCrash
Environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: