Skip to content

Commit

Permalink
Dispose media after playing.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohannesKauffmann committed Dec 23, 2020
1 parent ee472c0 commit c905cd3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 5 additions & 1 deletion OneDrive-Cloud-Player/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"]);
Expand All @@ -269,15 +268,15 @@ 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";
});
}

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.
Expand All @@ -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;
});
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 0 additions & 1 deletion OneDrive-Cloud-Player/Views/VideoPlayerPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit c905cd3

Please sign in to comment.