-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. Replaced public mediaplayer property calls with private.
- Loading branch information
Showing
11 changed files
with
500 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
OneDrive-Cloud-Player/Models/Interfaces/IMediaTrackService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using LibVLCSharp.Shared; | ||
using LibVLCSharp.Shared.Structures; | ||
using System.Threading.Tasks; | ||
|
||
namespace OneDrive_Cloud_Player.Models.Interfaces | ||
{ | ||
public interface IMediaTrackService | ||
{ | ||
/// <summary> | ||
/// Needs to be called before using the service and before subscribing to other libvlcsharp events.<br /> | ||
/// 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.<br /> | ||
/// Initialize this service by parsing the MediaPlayer object reference for its media tracks. | ||
/// </summary> | ||
/// <param name="mediaPlayer"></param> | ||
/// <returns></returns> | ||
IMediaTrackService Initialize(ref MediaPlayer mediaPlayer); | ||
|
||
/// <summary> | ||
/// Get the preferred subtitle track. When no preferred subtitle track is found, a default will be returned. | ||
/// <br /> | ||
/// <i>Note: This method should not be called on a LibVLC thread. | ||
/// <see href="https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/docs/best_practices.md#do-not-call-libvlc-from-a-libvlc-event-without-switching-thread-first">Here</see> is more info on why.</i> | ||
/// </summary> | ||
/// <returns><see cref="TrackDescription"/> Preferred subtitle track or default</returns> | ||
/// <exception cref="ServiceNotInitializedException"></exception> | ||
TrackDescription GetPreferredSubtitleTrack(); | ||
|
||
/// <summary> | ||
/// Get the embedded subtitle tracks in the media file. | ||
/// <br /> | ||
/// <i>Note: This method should not be called on a LibVLC thread. | ||
/// <see href="https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/docs/best_practices.md#do-not-call-libvlc-from-a-libvlc-event-without-switching-thread-first">Here</see> is more info on why.</i> | ||
/// </summary> | ||
/// <returns>Array containing <see cref="TrackDescription"/>'s or empty when no tracks are available</returns> | ||
TrackDescription[] GetEmbeddedSubtitleTracks(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
OneDrive-Cloud-Player/Services/Converters/ValueConverterGroup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using LibVLCSharp.Shared; | ||
using LibVLCSharp.Shared.Structures; | ||
using OneDrive_Cloud_Player.Models.Interfaces; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Windows.Storage; | ||
|
||
namespace OneDrive_Cloud_Player.Services | ||
{ | ||
/// <summary> | ||
/// Manage the media tracks of the libvlcsharp media player instance. | ||
/// </summary> | ||
public class MediaTrackService : IMediaTrackService | ||
{ | ||
private MediaPlayer _mediaPlayer; | ||
private bool _isInitialized = false; | ||
private readonly ApplicationDataContainer _userSettings; | ||
|
||
public MediaTrackService() | ||
{ | ||
_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) | ||
{ | ||
if (_isInitialized) | ||
{ | ||
throw new InvalidOperationException("Service already initialized!"); | ||
} | ||
|
||
Debug.WriteLine(String.Format("initializing {0}", GetType().Name)); | ||
_mediaPlayer = mediaPlayer; | ||
_isInitialized = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
private void CheckInitializationState() | ||
{ | ||
if (!_isInitialized) | ||
{ | ||
throw new InvalidOperationException("Service not initialized!"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <returns> <inheritdoc/></returns> | ||
public TrackDescription GetPreferredSubtitleTrack() | ||
{ | ||
CheckInitializationState(); | ||
TrackDescription[] subtitleTracks = _mediaPlayer.SpuDescription; | ||
|
||
if (_mediaPlayer.SpuCount <= 0) | ||
{ | ||
return default; | ||
} | ||
|
||
// Return default track subtitle (when possible, like in mkv) depending on the user setting. | ||
if ((bool)_userSettings.Values["ShowDefaultSubtitles"]) | ||
{ | ||
return subtitleTracks.First(subtitleTrack => subtitleTrack.Id == _mediaPlayer.Spu); | ||
} | ||
else | ||
{ | ||
// Return the "disabled" indicator track positioned at index 0. | ||
return _mediaPlayer.SpuDescription.ElementAt(0); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <returns><inheritdoc/></returns> | ||
public TrackDescription[] GetEmbeddedSubtitleTracks() | ||
{ | ||
CheckInitializationState(); | ||
return _mediaPlayer.SpuDescription; | ||
} | ||
} | ||
} |
Oops, something went wrong.