11using System . ComponentModel ;
22using CommunityToolkit . Maui . Core ;
33using CommunityToolkit . Maui . Extensions ;
4- using CommunityToolkit . Maui . Sample . Constants ;
54using CommunityToolkit . Maui . Sample . ViewModels . Views ;
65using CommunityToolkit . Maui . Views ;
76using Microsoft . Extensions . Logging ;
87
98namespace CommunityToolkit . Maui . Sample . Pages . Views ;
109
11- public partial class MediaElementFromStreamPage : BasePage < MediaElementFromStreamViewModel >
10+ public partial class MediaElementFromStreamPage : BasePage < MediaElementFromStreamViewModel > , IDisposable
1211{
13- const string loadOnlineMp4 = "Load Online MP4" ;
14- const string loadHls = "Load HTTP Live Stream (HLS)" ;
15- const string loadLocalResource = "Load Local Resource" ;
16- const string resetSource = "Reset Source to null" ;
17- const string loadMusic = "Load Music" ;
18-
1912 const string botImageUrl = "https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm" ;
20- const string hlsStreamTestUrl = "https://mtoczko.github.io/hls-test-streams/test-gap/playlist.m3u8" ;
21- const string hal9000AudioUrl = "https://github.com/prof3ssorSt3v3/media-sample-files/raw/master/hal-9000.mp3" ;
2213
2314 readonly ILogger logger ;
2415 readonly IDeviceInfo deviceInfo ;
25- readonly IFileSystem fileSystem ;
2616
27- MemoryStream sourceStream ;
17+ MemoryStream ? sourceStream ;
18+ bool disposed ;
2819
29- public MediaElementFromStreamPage ( MediaElementFromStreamViewModel viewModel , IFileSystem fileSystem , IDeviceInfo deviceInfo , ILogger < MediaElementPage > logger ) : base ( viewModel )
20+ public MediaElementFromStreamPage ( MediaElementFromStreamViewModel viewModel , IDeviceInfo deviceInfo , ILogger < MediaElementPage > logger ) : base ( viewModel )
3021 {
3122 InitializeComponent ( ) ;
3223
3324 this . logger = logger ;
3425 this . deviceInfo = deviceInfo ;
35- this . fileSystem = fileSystem ;
3626 MediaElement . PropertyChanged += MediaElement_PropertyChanged ;
3727 }
3828
@@ -276,4 +266,32 @@ async void DisplayPopup(object? sender, EventArgs? e)
276266 popupMediaElement . Stop ( ) ;
277267 popupMediaElement . Source = null ;
278268 }
269+
270+ protected override void OnDisappearing ( )
271+ {
272+ base . OnDisappearing ( ) ;
273+ sourceStream ? . Dispose ( ) ;
274+ }
275+
276+ public void Dispose ( )
277+ {
278+ Dispose ( true ) ;
279+ GC . SuppressFinalize ( this ) ;
280+ }
281+
282+ protected virtual void Dispose ( bool disposing )
283+ {
284+ if ( disposed )
285+ {
286+ return ;
287+ }
288+
289+ if ( disposing )
290+ {
291+ return ;
292+ }
293+
294+ disposed = true ;
295+ sourceStream ? . Dispose ( ) ;
296+ }
279297}
0 commit comments