From 39c45c050f9cd5120c1426eda22f2861f6356842 Mon Sep 17 00:00:00 2001 From: James Crutchley Date: Mon, 7 Oct 2024 12:39:43 -0700 Subject: [PATCH] Enhance Dispose method for better resource management Updated the Dispose method in MauiMediaElement.windows.cs to include: - Pausing the media player before disposal. - Explicitly disposing of MediaSource if used. - Setting the media player's source to null. - Disposing of the media player. - Setting the media player element's media player to null. These changes ensure proper release of resources, preventing memory leaks. --- .../Views/MauiMediaElement.windows.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs index edac52be68..c38e3ab3fc 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs @@ -105,7 +105,17 @@ protected virtual void Dispose(bool disposing) if (disposing) { + mediaPlayerElement.MediaPlayer.Pause(); + + if(mediaPlayerElement.MediaPlayer.Source is Windows.Media.Core.MediaSource mediaSource) + { + // Dispose the MediaSource to release the resources + // https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/play-audio-and-video-with-mediaplayer Shows how to dispose the MediaSource + mediaSource.Dispose(); + } + mediaPlayerElement.MediaPlayer.Source = null; mediaPlayerElement.MediaPlayer.Dispose(); + mediaPlayerElement.SetMediaPlayer(null); } isDisposed = true;