From c905cd3b9d657e6c2dd9deb76dedbae9a7329639 Mon Sep 17 00:00:00 2001 From: JohannesKauffmann <19662702+JohannesKauffmann@users.noreply.github.com> Date: Thu, 24 Dec 2020 00:12:49 +0100 Subject: [PATCH] Dispose media after playing. Related issue is #40. App.xaml.cs: - add: Global UI dispatcher property, so every class can access the UI conventiently. VideoPlayerPageViewModel.cs: - rem: Unneeded using system.diagnostics. VideoPlayerPage.xaml.cs: - chg: Dispose the media after playing it by wrapping it in a using. This follows the official LibVLCSharp UWP sample. Replaced lengthy dispatcher calls with App.Current. --- OneDrive-Cloud-Player/App.xaml.cs | 6 +++++- .../ViewModels/VideoPlayerPageViewModel.cs | 14 ++++++++------ .../Views/VideoPlayerPage.xaml.cs | 1 - 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/OneDrive-Cloud-Player/App.xaml.cs b/OneDrive-Cloud-Player/App.xaml.cs index 76f3bb7..50bd6fe 100644 --- a/OneDrive-Cloud-Player/App.xaml.cs +++ b/OneDrive-Cloud-Player/App.xaml.cs @@ -9,6 +9,8 @@ using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; +using Windows.ApplicationModel.Core; +using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; @@ -22,7 +24,7 @@ sealed partial class App : Application { //Other classes can now call this class with the use of 'App.Current'. public static new App Current => (App)Application.Current; - + public CoreDispatcher UIDispatcher { get; private set; } public IPublicClientApplication PublicClientApplication { get; private set; } public string[] Scopes { get; private set; } public CacheHelper CacheHelper { get; private set; } @@ -111,6 +113,8 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e) // Ensure the current window is active Window.Current.Activate(); } + + UIDispatcher = CoreApplication.MainView.CoreWindow.Dispatcher; } /// diff --git a/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs b/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs index bc39dc7..eae5b85 100644 --- a/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs +++ b/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs @@ -12,7 +12,6 @@ using System.Threading.Tasks; using System.Timers; using System.Windows.Input; -using Windows.ApplicationModel.Core; using Windows.Storage; using Windows.System; using Windows.UI.Core; @@ -254,7 +253,7 @@ private async void InitializeLibVLC(InitializedEventArgs eventArgs) private async void MediaPlayer_Playing(object sender, EventArgs e) { Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Media is playing"); - await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => + await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.High, () => { MediaVolumeLevel = (int)this.localMediaVolumeLevelSetting.Values["MediaVolume"]; Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Set volume in container: " + this.localMediaVolumeLevelSetting.Values["MediaVolume"]); @@ -269,7 +268,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio private async void MediaPlayer_Paused(object sender, EventArgs e) { - await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => + await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PlayPauseButtonFontIcon = "\xE768"; }); @@ -277,7 +276,7 @@ await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPrio private async void MediaPlayer_TimeChanged(object sender, EventArgs e) { - await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => + await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.Low, () => { // Updates the value of the seekbar on TimeChanged event // when the user is not seeking. @@ -300,7 +299,7 @@ private void ReloadIntervalTimer_Elapsed(object sender, ElapsedEventArgs e) private async void FileNameOverlayTimer_Elapsed(object sender, ElapsedEventArgs e) { - await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => + await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.Low, () => { FileNameOverlayVisiblity = Visibility.Collapsed; }); @@ -338,7 +337,10 @@ private async Task PlayMedia(long startTime = 0) string mediaDownloadURL = await RetrieveDownloadURLMedia(MediaWrapper); // Play the OneDrive file. - MediaPlayer.Play(new Media(LibVLC, new Uri(mediaDownloadURL))); + using (Media media = new Media(LibVLC, new Uri(mediaDownloadURL))) + { + MediaPlayer.Play(media); + } if (MediaPlayer is null) { diff --git a/OneDrive-Cloud-Player/Views/VideoPlayerPage.xaml.cs b/OneDrive-Cloud-Player/Views/VideoPlayerPage.xaml.cs index b0474d7..9e4e56f 100644 --- a/OneDrive-Cloud-Player/Views/VideoPlayerPage.xaml.cs +++ b/OneDrive-Cloud-Player/Views/VideoPlayerPage.xaml.cs @@ -1,6 +1,5 @@ using OneDrive_Cloud_Player.ViewModels; using System; -using System.Diagnostics; using Windows.System; using Windows.UI.Core; using Windows.UI.ViewManagement;