85 lines
2.2 KiB
C#
85 lines
2.2 KiB
C#
using LibVLCSharp.Shared;
|
|
using TechMedia.Core.Backend;
|
|
using TechMedia.Core.Model;
|
|
|
|
namespace TechMedia.Backend.Vlc;
|
|
|
|
public class VlcAudioBackend : IAudioBackend
|
|
{
|
|
private readonly LibVLC _libVlc;
|
|
private readonly MediaPlayer _player;
|
|
private readonly Dictionary<IProgress<float>, EventHandler<MediaPlayerPositionChangedEventArgs>> _progressReporters = new();
|
|
|
|
public VlcAudioBackend()
|
|
{
|
|
LibVLCSharp.Shared.Core.Initialize();
|
|
_libVlc = new LibVLC();
|
|
_player = new MediaPlayer(_libVlc);
|
|
}
|
|
|
|
public void JustPlay()
|
|
{
|
|
_player.Play();
|
|
}
|
|
|
|
public void PlayFile(string filename)
|
|
{
|
|
//_player.Stop();
|
|
var media = new Media(_libVlc, filename);
|
|
_player.Media = media;
|
|
|
|
media.Dispose();
|
|
|
|
_player.Play();
|
|
//_player.Position = 0.97f;
|
|
}
|
|
|
|
public async Task<Track?> GetFileMetadata(string filename)
|
|
{
|
|
using var media = new Media(_libVlc, filename);
|
|
await Task.Run(async () => await media.Parse());
|
|
if (media.Duration <= 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new Track
|
|
{
|
|
Title = media.Meta(MetadataType.Title),
|
|
Album = media.Meta(MetadataType.Album),
|
|
AlbumArt = media.Meta(MetadataType.ArtworkURL),
|
|
Duration = media.Duration,
|
|
Filename = filename
|
|
};
|
|
}
|
|
|
|
public void StopPlayback()
|
|
{
|
|
_player.Stop();
|
|
}
|
|
|
|
public void RegisterProgressWatcher(IProgress<float> reporter)
|
|
{
|
|
void Handler(object? _, MediaPlayerPositionChangedEventArgs args)
|
|
{
|
|
reporter.Report(args.Position);
|
|
}
|
|
|
|
_player.PositionChanged += Handler;
|
|
_progressReporters.Add(reporter, Handler);
|
|
}
|
|
|
|
public void RemoveProgressWatcher(IProgress<float> reporter)
|
|
{
|
|
if (_progressReporters.ContainsKey(reporter))
|
|
{
|
|
_player.PositionChanged -= _progressReporters[reporter];
|
|
_progressReporters.Remove(reporter);
|
|
}
|
|
}
|
|
|
|
public void RegisterTrackEndHandler(EventHandler<EventArgs> onEndReached)
|
|
{
|
|
_player.EndReached += onEndReached;
|
|
}
|
|
} |