From bf6eed093d52226d4d5ed00b35e57c96df0aced1 Mon Sep 17 00:00:00 2001
From: Johannes Kauffmann <19662702+JohannesKauffmann@users.noreply.github.com>
Date: Mon, 13 Mar 2023 22:40:59 +0100
Subject: [PATCH] SQUASH
---
.../ServiceNotInitializedException.cs | 13 -----------
.../Models/Interfaces/IMediaTrackService.cs | 1 -
.../OneDrive-Cloud-Player.csproj | 1 -
.../Services/MediaTrackService.cs | 15 ++++++++-----
.../ViewModels/VideoPlayerPageViewModel.cs | 22 +++++++++----------
5 files changed, 20 insertions(+), 32 deletions(-)
delete mode 100644 OneDrive-Cloud-Player/Exceptions/ServiceNotInitializedException.cs
diff --git a/OneDrive-Cloud-Player/Exceptions/ServiceNotInitializedException.cs b/OneDrive-Cloud-Player/Exceptions/ServiceNotInitializedException.cs
deleted file mode 100644
index 1e352a8..0000000
--- a/OneDrive-Cloud-Player/Exceptions/ServiceNotInitializedException.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using System;
-
-namespace OneDrive_Cloud_Player.Exceptions
-{
- internal class ServiceNotInitializedException : Exception
- {
- public ServiceNotInitializedException() { }
-
- public ServiceNotInitializedException(string message) : base(message) { }
-
- public ServiceNotInitializedException(string message, Exception inner) : base(message, inner) { }
- }
-}
diff --git a/OneDrive-Cloud-Player/Models/Interfaces/IMediaTrackService.cs b/OneDrive-Cloud-Player/Models/Interfaces/IMediaTrackService.cs
index 1fb8494..849a5db 100644
--- a/OneDrive-Cloud-Player/Models/Interfaces/IMediaTrackService.cs
+++ b/OneDrive-Cloud-Player/Models/Interfaces/IMediaTrackService.cs
@@ -1,6 +1,5 @@
using LibVLCSharp.Shared;
using LibVLCSharp.Shared.Structures;
-using OneDrive_Cloud_Player.Exceptions;
namespace OneDrive_Cloud_Player.Models.Interfaces
{
diff --git a/OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj b/OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj
index 42b9369..d0d4b1d 100644
--- a/OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj
+++ b/OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj
@@ -127,7 +127,6 @@
App.xaml
-
diff --git a/OneDrive-Cloud-Player/Services/MediaTrackService.cs b/OneDrive-Cloud-Player/Services/MediaTrackService.cs
index 2033362..c8462dc 100644
--- a/OneDrive-Cloud-Player/Services/MediaTrackService.cs
+++ b/OneDrive-Cloud-Player/Services/MediaTrackService.cs
@@ -1,7 +1,7 @@
using LibVLCSharp.Shared;
using LibVLCSharp.Shared.Structures;
-using OneDrive_Cloud_Player.Exceptions;
using OneDrive_Cloud_Player.Models.Interfaces;
+using System;
using System.Diagnostics;
using System.Linq;
using Windows.Storage;
@@ -28,7 +28,12 @@ public MediaTrackService()
///
public IMediaTrackService Initialize(ref MediaPlayer mediaPlayer)
{
- Debug.WriteLine("initializing MediaTrackService");
+ if (_isInitialized)
+ {
+ throw new InvalidOperationException("Service already initialized!");
+ }
+
+ Debug.WriteLine("initializing {0}", this.GetType().Name);
_mediaPlayer = mediaPlayer;
_isInitialized = true;
return this;
@@ -37,12 +42,12 @@ public IMediaTrackService Initialize(ref MediaPlayer mediaPlayer)
///
///
///
- ///
+ ///
private void CheckInitializationState()
{
if (!_isInitialized)
{
- throw new ServiceNotInitializedException();
+ throw new InvalidOperationException("Service not initialized!");
}
}
@@ -57,7 +62,7 @@ public TrackDescription GetPreferredSubtitleTrack()
//Enable or disable default subtitle based on user setting.
if (!(bool)_userSettings.Values["ShowDefaultSubtitles"])
{
- return _mediaPlayer.SpuDescription.ElementAtOrDefault(0);
+ return default;
}
return _mediaPlayer.SpuDescription.ElementAtOrDefault(1);
diff --git a/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs b/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs
index f9ba0ac..2430f61 100644
--- a/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs
+++ b/OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs
@@ -285,7 +285,7 @@ private async void InitializeLibVLC(InitializedEventArgs eventArgs)
// Create LibVLC instance.
LibVLC = new LibVLC(eventArgs.SwapChainOptions);
- MediaPlayer = new MediaPlayer(LibVLC);
+ _mediaPlayer = new MediaPlayer(LibVLC);
_mediaTrackService.Initialize(ref _mediaPlayer);
// Subscribe to events only once.
_mediaPlayer.Playing += MediaPlayer_Playing;
@@ -344,19 +344,16 @@ await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.High, () =>
private async void MediaPlayer_Playing(object sender, EventArgs e)
{
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Media is playing");
+
if (_isFirstPlaying || _isReloading)
{
- TrackDescription[] subtitleTracks;
- TrackDescription selectedSubtitleTrack;
+ TrackDescription[] subtitleTracks = null;
+ TrackDescription selectedSubtitleTrack = default;
- subtitleTracks = await Task.Run(() =>
+ await Task.Run(() =>
{
- return _mediaTrackService.GetEmbeddedSubtitleTracks();
- });
-
- selectedSubtitleTrack = await Task.Run(() =>
- {
- return _mediaTrackService.GetPreferredSubtitleTrack();
+ subtitleTracks = _mediaTrackService.GetEmbeddedSubtitleTracks();
+ selectedSubtitleTrack = _mediaTrackService.GetPreferredSubtitleTrack();
});
if (_isFirstPlaying)
@@ -367,6 +364,7 @@ private async void MediaPlayer_Playing(object sender, EventArgs e)
if (_isReloading)
{
_isReloading = false;
+ // Retrieve the previously selected subtitle track.
selectedSubtitleTrack = subtitleTracks.FirstOrDefault(subtitleTrack => subtitleTrack.Id == _selectedSubtitleTrack.Id);
}
@@ -500,10 +498,10 @@ private void SetSubtitleTrackById(int subtitleTrackId)
private void SetMediaVolume(int volumeLevel)
{
- if (MediaPlayer is null)
+ if (_mediaPlayer is null)
{
Debug.WriteLine("Error: SetMediaVolumeLevel: Could not set the volume.");
- return; // Return when the MediaPlayer is null so it does not cause exception.
+ return; // Return when the _mediaPlayer is null so it does not cause exception.
}
App.Current.UserSettings.Values["MediaVolume"] = volumeLevel; // Set the new volume in the MediaVolume setting.
_mediaPlayer.Volume = volumeLevel;