Skip to content

Commit 3e8586f

Browse files
Fix Media Element fails to load a URL (#2265)
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. Co-authored-by: Brandon Minnick <[email protected]> Co-authored-by: Brandon Minnick <[email protected]>
1 parent b4183e1 commit 3e8586f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ protected virtual void Dispose(bool disposing)
139139

140140
if (disposing)
141141
{
142+
mediaPlayerElement.MediaPlayer.Pause();
143+
144+
if(mediaPlayerElement.MediaPlayer.Source is Windows.Media.Core.MediaSource mediaSource)
145+
{
146+
// Dispose the MediaSource to release the resources
147+
// https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/play-audio-and-video-with-mediaplayer Shows how to dispose the MediaSource
148+
mediaSource.Dispose();
149+
}
150+
mediaPlayerElement.MediaPlayer.Source = null;
142151
mediaPlayerElement.MediaPlayer.Dispose();
152+
mediaPlayerElement.SetMediaPlayer(null);
143153
}
144154

145155
isDisposed = true;

0 commit comments

Comments
 (0)