Initial commit
This commit is contained in:
18
TechMedia.Backend.Vlc/TechMedia.Backend.Vlc.csproj
Normal file
18
TechMedia.Backend.Vlc/TechMedia.Backend.Vlc.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TechMedia.Core\TechMedia.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LibVLCSharp" Version="3.6.5" />
|
||||
<PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.16" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
85
TechMedia.Backend.Vlc/VlcAudioBackend.cs
Normal file
85
TechMedia.Backend.Vlc/VlcAudioBackend.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user