Skip to content

Commit

Permalink
Added Subtitle Track selector
Browse files Browse the repository at this point in the history
Implemented the ability to select a subtitle track on the video player
page.

Added a SubtitleAudioTrackSelector UserControl. This UserControl hosts
the subtitle tracks and will also host the audio tracks from which a
user can select a track.

Introduced a MediaTrackService which can be injected to provider some
methods. Methods it provide are at this moment the retrieval of subtitle
tracks and the preferred subtitle track. Due to LibVLC not being able to
be injected by a dependency injector due to several reasons, this
service needs to be initialized by calling the initialize method and
providing it with a reference to a (initialized!) MediaPlayer instance. This service can be expanded to
perform more general MediaPlayer tasks as well in the future if need be.

Added a ValueConverterGroup enabling the chaining of value converters.

Added a custom exception which gets thrown when one of the the MediaTrackService
methods get called without the service being initialized.

Added MediaTrackService to the dependency Injector container in App.xaml.cs.
  • Loading branch information
TimGels committed Mar 11, 2023
1 parent 4520023 commit 0941b9c
Show file tree
Hide file tree
Showing 14 changed files with 431 additions and 14 deletions.
4 changes: 2 additions & 2 deletions OneDrive-Cloud-Player.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29418.71
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneDrive-Cloud-Player", "OneDrive-Cloud-Player\OneDrive-Cloud-Player.csproj", "{A2003B0F-47A8-495C-ABD2-A7A38148C2D3}"
EndProject
Expand Down
2 changes: 2 additions & 0 deletions OneDrive-Cloud-Player/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Client;
using OneDrive_Cloud_Player.Models.GraphData;
using OneDrive_Cloud_Player.Models.Interfaces;
using OneDrive_Cloud_Player.Services;
using OneDrive_Cloud_Player.Views;
using System;
Expand Down Expand Up @@ -83,6 +84,7 @@ IServiceProvider ConfigureDependencyInjection()
var serviceCollection = new ServiceCollection();

// Add services for dependency injection here.
serviceCollection.AddTransient<IMediaTrackService, MediaTrackService>();

return serviceCollection.BuildServiceProvider();
}
Expand Down
13 changes: 13 additions & 0 deletions OneDrive-Cloud-Player/Exceptions/ServiceNotInitializedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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) { }
}
}
17 changes: 17 additions & 0 deletions OneDrive-Cloud-Player/Models/Interfaces/IMediaTrackService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using LibVLCSharp.Shared;
using LibVLCSharp.Shared.Structures;

namespace OneDrive_Cloud_Player.Models.Interfaces
{
public interface IMediaTrackService
{
/// <summary>
/// Needs to be called before using the service.
/// </summary>
/// <param name="mediaPlayer"></param>
/// <returns></returns>
IMediaTrackService Initialize(ref MediaPlayer mediaPlayer);
TrackDescription GetPreferredSubtitleTrack();
TrackDescription[] GetSubtitleTracks();
}
}
20 changes: 20 additions & 0 deletions OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,22 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Exceptions\ServiceNotInitializedException.cs" />
<Compile Include="Models\GraphData\CachedDrive.cs" />
<Compile Include="Models\GraphData\CachedDriveItem.cs" />
<Compile Include="Models\GraphData\OneDriveCache.cs" />
<Compile Include="Models\Interfaces\IMediaTrackService.cs" />
<Compile Include="Models\Interfaces\INavigable.cs" />
<Compile Include="Models\MediaWrapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\Converters\MainPageExplorerConverter.cs" />
<Compile Include="Services\Converters\MediaTimeConverter.cs" />
<Compile Include="Services\Converters\ValueConverterGroup.cs" />
<Compile Include="Services\Helpers\CacheHelper.cs" />
<Compile Include="Services\Helpers\GraphHelper.cs" />
<Compile Include="Services\Helpers\GraphAuthHelper.cs" />
<Compile Include="Services\NavigationService.cs" />
<Compile Include="Services\MediaTrackService.cs" />
<Compile Include="Services\Utilities\JsonHandler.cs" />
<Compile Include="ViewModels\LoginPageViewModel.cs" />
<Compile Include="ViewModels\MainPageViewModel.cs" />
Expand All @@ -154,6 +158,9 @@
<Compile Include="Views\SettingsPage.xaml.cs">
<DependentUpon>SettingsPage.xaml</DependentUpon>
</Compile>
<Compile Include="Views\SubtitleAudioTrackSelector.xaml.cs">
<DependentUpon>SubtitleAudioTrackSelector.xaml</DependentUpon>
</Compile>
<Compile Include="Views\VideoPlayerPage.xaml.cs">
<DependentUpon>VideoPlayerPage.xaml</DependentUpon>
</Compile>
Expand All @@ -164,6 +171,9 @@
</AppxManifest>
</ItemGroup>
<ItemGroup>
<None Include="..\.editorconfig">
<Link>.editorconfig</Link>
</None>
<None Include="Models\GraphData\graphcache.json" />
<Resource Include="Assets\Fonts\Lato-Bold.ttf" />
<Resource Include="Assets\Fonts\Lato-Light.ttf" />
Expand Down Expand Up @@ -250,6 +260,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\SubtitleAudioTrackSelector.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\VideoPlayerPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -277,6 +291,12 @@
<PackageReference Include="Microsoft.Toolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp.UI">
<Version>7.1.3</Version>
</PackageReference>
<PackageReference Include="Microsoft.UI.Xaml">
<Version>2.8.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OneDrive_Cloud_Player.Services.Converters
class MediaTimeConverter : IValueConverter
{
object IValueConverter.Convert(object timeMs, Type targetType, object parameter, string language)
{
{
TimeSpan timeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble(timeMs));
return timeSpan.ToString(@"hh\:mm\:ss");
}
Expand Down
20 changes: 20 additions & 0 deletions OneDrive-Cloud-Player/Services/Converters/ValueConverterGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml.Data;

namespace OneDrive_Cloud_Player.Services.Converters
{
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return this.Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, language));
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
68 changes: 68 additions & 0 deletions OneDrive-Cloud-Player/Services/MediaTrackService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Diagnostics;
using System.Linq;
using LibVLCSharp.Shared;
using LibVLCSharp.Shared.Structures;
using OneDrive_Cloud_Player.Exceptions;
using OneDrive_Cloud_Player.Models.Interfaces;
using Windows.Storage;

namespace OneDrive_Cloud_Player.Services
{
public class MediaTrackService :IMediaTrackService
{
private MediaPlayer _mediaPlayer;
private bool _isInitialized = false;
private readonly ApplicationDataContainer _UserSettings;

/// <summary>
/// It is important to always initialize this service again after the mediaplayer changes (i.e. after a reload);
/// </summary>
public MediaTrackService()
{
this._UserSettings = ApplicationData.Current.LocalSettings;
}

/// <summary>
/// Due to LibVLCSharp's inability for the MediaPlayer to be put into a service, we need to get a reference to the MediaPlayer ourselves instead.
/// Initialize this service by parsing the MediaPlayer object reference for its media tracks.
/// </summary>
public IMediaTrackService Initialize(ref MediaPlayer mediaPlayer)
{
Debug.WriteLine("initializing service");
_mediaPlayer = mediaPlayer;
_isInitialized = true;
return this;
}

private void CheckInitializationState()
{
if (!_isInitialized)
{
throw new ServiceNotInitializedException();
}
}

/// <summary>
/// Get the preferred subtitle track. When no preferred subtitle track is found, a default will be returned.
/// </summary>
/// <returns><see cref="TrackDescription"/> Preferred subtitle track or default</returns>
public TrackDescription GetPreferredSubtitleTrack()
{
CheckInitializationState();

//Enable or disable default subtitle based on user setting.
if (!(bool)_UserSettings.Values["ShowDefaultSubtitles"])
{
return _mediaPlayer.SpuDescription.ElementAtOrDefault(0);
}

return _mediaPlayer.SpuDescription.ElementAtOrDefault(1);
}

public TrackDescription[] GetSubtitleTracks()
{
CheckInitializationState();
return _mediaPlayer.SpuDescription;
}
}
}
1 change: 1 addition & 0 deletions OneDrive-Cloud-Player/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Toolkit.Mvvm.Input;
using OneDrive_Cloud_Player.Models;
using OneDrive_Cloud_Player.Models.GraphData;
using OneDrive_Cloud_Player.Models.Interfaces;
using OneDrive_Cloud_Player.Services;
using OneDrive_Cloud_Player.Services.Helpers;
using OneDrive_Cloud_Player.Views;
Expand Down
Loading

0 comments on commit 0941b9c

Please sign in to comment.