Maui: [BUG] Crash when cleaning up MediaElement on Windows

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

  1. Follow the guide at MediaElement announcement blog post to create a very simple page to play a movie from an internet uri.
  2. Start app on net7.0-windows10.0.19041.0
  3. Navigate to the movie page
  4. Browse back

Expected Behavior

App navigates back and resources are cleaned up

Steps To Reproduce

App navigates back and a crash occurs

System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (0x80004004 (E_ABORT))
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Windows.Media.Playback.IMediaPlayerMethods.get_Position(IObjectReference _obj)
   at Windows.Media.Playback.MediaPlayer.get_Position()
   at CommunityToolkit.Maui.Core.Views.MediaManager.PlatformUpdatePosition()
   at CommunityToolkit.Maui.Core.Views.MediaManager.UpdateStatus()
   at CommunityToolkit.Maui.Core.Handlers.MediaElementHandler.MapStatusUpdated(MediaElementHandler handler, MediaElement MediaElement, Object args)
   at Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args)
   at CommunityToolkit.Maui.Views.MediaElement.OnTimerTick(Object sender, EventArgs e)
   at Microsoft.Maui.Dispatching.DispatcherTimer.OnTimerTick(DispatcherQueueTimer sender, Object args)
   at WinRT._EventSource_global__Windows_Foundation_TypedEventHandler_global__Microsoft_UI_Dispatching_Dispatcher
QueueTimer__object_.EventState.<GetEventInvoke>b__1_0(DispatcherQueueTimer sender, Object args)
   at ABI.Windows.Foundation.TypedEventHandler`2.Do_Abi_Invoke[TSenderAbi,TResultAbi](Void* thisPtr, TSenderAbi sender, TResultAbi args)

Link to public reproduction project repository

https://github.com/wldevries/MauiMediaElementCrash

Environment

- .NET MAUI CommunityToolkit: 4.0.0
- OS: Windows 10 Build 10.0.19045.2546
- .NET MAUI: no idea.. part of VS 2022 17.4.4
- .NET MAUI MediaElement: 1.0.1

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Tested with version 1.0.0-build-1002.87588 and DiconnectHander doesn’t throw exception anymore.

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.

I had same issue till I did a null check for sender like this.

inside xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:viewmodel="clr-namespace:NerdNewsNavigator2.ViewModel.Desktop"
                xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="NerdNewsNavigator2.View.Desktop.DesktopPlayPodcastPage"
              x:DataType="viewmodel:DesktopPlayPodcastViewModel"
             Shell.NavBarIsVisible="False"
             Title=""
             Unloaded="ContentPage_Unloaded">
    <Shell.BackButtonBehavior>
        <BackButtonBehavior IsEnabled="True" IsVisible="False">
        </BackButtonBehavior>
    </Shell.BackButtonBehavior>
    
        <Grid>
        <toolkit:MediaElement
               x:Name="mediaElement"
                ShouldAutoPlay="True"
                ShouldShowPlaybackControls="True"
                Source="{Binding Url}"
               />
    </Grid>
</ContentPage>

inside xaml.page.cs

 void ContentPage_Unloaded(object? sender, EventArgs e)
    {
        if (sender is null)
        {
            return;
        }
        // Stop and cleanup MediaElement when we navigate away
        mediaElement.Handler?.DisconnectHandler();
    }

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:

Unloaded="ContentPage_Unloaded">