mirror of
https://github.com/Royce551/FRESHMusicPlayer-Core.git
synced 2025-01-22 10:51:56 -05:00
parent
3561317f1a
commit
126b40e973
6 changed files with 26 additions and 61 deletions
|
@ -3,48 +3,18 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
namespace FRESHMusicPlayer.Backends
|
||||
{
|
||||
public class FileMetadataProvider : IMetadataProvider
|
||||
{
|
||||
public string Title => ATLTrack.Title;
|
||||
// for mp3s, ATL still uses /
|
||||
// for mp3s, ATL still uses /
|
||||
public string[] Artists => ATLTrack.Artist.Split(Settings.DisplayValueSeparator, '/');
|
||||
|
||||
public string Album => ATLTrack.Album;
|
||||
|
||||
public byte[] CoverArt
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ATLTrack.EmbeddedPictures.Count != 0) return ATLTrack.EmbeddedPictures[0].PictureData;
|
||||
else // no embedded cover art? fall back to getting it from the directory the audio file is in
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
var currentDirectory = Path.GetDirectoryName(path);
|
||||
foreach (var file in Directory.EnumerateFiles(currentDirectory))
|
||||
{
|
||||
if (Path.GetFileNameWithoutExtension(file).ToUpper() == "COVER" ||
|
||||
Path.GetFileNameWithoutExtension(file).ToUpper() == "ARTWORK" ||
|
||||
Path.GetFileNameWithoutExtension(file).ToUpper() == "FRONT" ||
|
||||
Path.GetFileNameWithoutExtension(file).ToUpper() == "BACK" ||
|
||||
Path.GetFileNameWithoutExtension(file).ToUpper() == path)
|
||||
{
|
||||
if (Path.GetExtension(file) == ".png" || Path.GetExtension(file) == ".jpg" || Path.GetExtension(file) == ".jpeg")
|
||||
{
|
||||
return File.ReadAllBytes(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public byte[] CoverArt => ATLTrack.EmbeddedPictures.Count != 0 ? ATLTrack.EmbeddedPictures[0].PictureData : null;
|
||||
|
||||
public string[] Genres => ATLTrack.Genre.Split(Settings.DisplayValueSeparator, '/');
|
||||
|
||||
|
@ -60,11 +30,6 @@ namespace FRESHMusicPlayer.Backends
|
|||
|
||||
public Track ATLTrack { get; set; }
|
||||
|
||||
private string path;
|
||||
public FileMetadataProvider(string path)
|
||||
{
|
||||
this.path = path;
|
||||
ATLTrack = new Track(path);
|
||||
}
|
||||
public FileMetadataProvider(string path) => ATLTrack = new Track(path);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,14 +9,14 @@ namespace FRESHMusicPlayer.Backends
|
|||
public interface IAudioBackend : IDisposable
|
||||
{
|
||||
Task<BackendLoadResult> LoadSongAsync(string file);
|
||||
Task<IMetadataProvider> LoadMetadataAsync(string file);
|
||||
|
||||
void Play();
|
||||
void Pause();
|
||||
|
||||
TimeSpan CurrentTime { get; set; }
|
||||
TimeSpan TotalTime { get; }
|
||||
|
||||
IMetadataProvider Metadata { get; }
|
||||
|
||||
float Volume { get; set; }
|
||||
|
||||
event EventHandler<EventArgs> OnPlaybackStopped;
|
||||
|
|
|
@ -30,6 +30,8 @@ namespace FRESHMusicPlayer.Backends
|
|||
}
|
||||
public TimeSpan TotalTime => AudioFile.TotalTime;
|
||||
|
||||
public IMetadataProvider Metadata { get; private set; }
|
||||
|
||||
public NAudioBackend()
|
||||
{
|
||||
if (OutputDevice is null)
|
||||
|
@ -51,6 +53,8 @@ namespace FRESHMusicPlayer.Backends
|
|||
{
|
||||
AudioFile = new AudioFileReader(file);
|
||||
OutputDevice.Init(AudioFile);
|
||||
|
||||
Metadata = new FileMetadataProvider(file);
|
||||
});
|
||||
}
|
||||
catch (ArgumentException)
|
||||
|
@ -72,8 +76,6 @@ namespace FRESHMusicPlayer.Backends
|
|||
return BackendLoadResult.OK;
|
||||
}
|
||||
|
||||
public async Task<IMetadataProvider> LoadMetadataAsync(string file) => await Task.Run(() => new FileMetadataProvider(file));
|
||||
|
||||
public void Play()
|
||||
{
|
||||
OutputDevice.Play();
|
||||
|
|
|
@ -119,6 +119,15 @@ namespace FRESHMusicPlayer
|
|||
AvoidNextQueue = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Repositions the playback position of the player.
|
||||
/// </summary>
|
||||
/// <param name="seconds">The position in to the track to skip in, in seconds.</param>
|
||||
public void RepositionMusic(int seconds)
|
||||
{
|
||||
CurrentBackend.CurrentTime = TimeSpan.FromSeconds(seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Plays a track. This is equivalent to calling Queue.Add() and then PlayMusic()./>
|
||||
/// </summary>
|
||||
|
|
|
@ -21,6 +21,8 @@ namespace FmpBassBackend
|
|||
|
||||
public event EventHandler<EventArgs> OnPlaybackStopped;
|
||||
|
||||
public IMetadataProvider Metadata { get; private set; }
|
||||
|
||||
private readonly MediaPlayer player = new MediaPlayer();
|
||||
|
||||
public FmpBassBackend()
|
||||
|
@ -41,12 +43,12 @@ namespace FmpBassBackend
|
|||
{
|
||||
var wasSuccessful = await player.LoadAsync(file);
|
||||
|
||||
Metadata = new FileMetadataProvider(file);
|
||||
|
||||
if (!wasSuccessful) return BackendLoadResult.Invalid;
|
||||
else return BackendLoadResult.OK;
|
||||
}
|
||||
|
||||
public async Task<IMetadataProvider> LoadMetadataAsync(string file) => await Task.Run(() => new FileMetadataProvider(file));
|
||||
|
||||
public void Pause() => player.Pause();
|
||||
|
||||
public void Play() => player.Play();
|
||||
|
|
|
@ -22,6 +22,8 @@ namespace FmpCdLibBackend
|
|||
|
||||
public float Volume { get => (float)player.Volume; set => player.Volume = value; }
|
||||
|
||||
public IMetadataProvider Metadata { get; private set; }
|
||||
|
||||
public event EventHandler<EventArgs> OnPlaybackStopped;
|
||||
|
||||
public FmpCdLibBackend()
|
||||
|
@ -69,26 +71,11 @@ namespace FmpCdLibBackend
|
|||
result = BackendLoadResult.OK;
|
||||
}
|
||||
}
|
||||
|
||||
if (trackToPlay != null) Metadata = new CDLibMetadataProvider(trackToPlay);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IMetadataProvider> LoadMetadataAsync(string file)
|
||||
{
|
||||
if (Path.GetExtension(file).ToUpper() != ".CDA") return null;
|
||||
|
||||
IAudioCDTrack trackToPlay = null;
|
||||
// super hacky; assumes that the path is something like D:\Track01.cda, might be a better way to do this
|
||||
var driveLetter = char.Parse(file.Substring(0, 1));
|
||||
var trackNumber = int.Parse(file.Substring(8, 2));
|
||||
|
||||
var drives = player.GetDrives();
|
||||
foreach (var drive in drives)
|
||||
{
|
||||
if (drive.DriveLetter == driveLetter) trackToPlay = drive.InsertedMedia.Tracks[trackNumber - 1];
|
||||
}
|
||||
return new CDLibMetadataProvider(trackToPlay);
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
player.Pause();
|
||||
|
|
Loading…
Reference in a new issue