using System.ComponentModel;
using System.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
namespace The_Untamed_Music_Player.Models;
public class BriefMusicInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string _path = "";
///
/// 文件位置
///
public string Path
{
get => _path;
set => _path = value;
}
private string _itemType = "";
///
/// 项目类型
///
public string ItemType
{
get => _itemType;
set => _itemType = value;
}
private string _album = "";
///
/// 专辑名, 为空时返回"未知专辑"
///
public string Album
{
get => _album;
set => _album = value;
}
private string _title = "";
///
/// 歌曲名
///
public string Title
{
get => _title;
set => _title = value;
}
private string[] _artists = [];
///
/// 参与创作的艺术家数组
///
public string[] Artists
{
get => _artists;
set
{
if (value?.Length > 0)
{
var tempArtists = Array.Empty();
foreach (var i in value)
{
if (i.Contains('、'))
{
tempArtists = [.. tempArtists, .. i.Split('、')];//将艺术家名字分割开并存到数组中
}
else if (i.Contains(','))
{
tempArtists = [.. tempArtists, .. i.Split(',')];
}
else if (i.Contains(','))
{
tempArtists = [.. tempArtists, .. i.Split(',')];
}
else if (i.Contains('|'))
{
tempArtists = [.. tempArtists, .. i.Split('|')];
}
else if (i.Contains('/'))
{
tempArtists = [.. tempArtists, .. i.Split('/')];
}
else
{
tempArtists = [.. tempArtists, .. new[] { i }];
}
}
_artists = tempArtists.Distinct().ToArray();
}
else
{
_artists = [];
}
ArtistsStr = GetArtists();
}
}
private string _artistsStr = "";
///
/// 参与创作的艺术家名, 为空时返回"未知艺术家"
///
public string ArtistsStr
{
get => _artistsStr;
set => _artistsStr = value;
}
///
/// 获取参与创作的艺术家名
///
///
public string GetArtists()
{
if (_artists == null || _artists.Length == 0)
{
return "未知艺术家";
}
var sb = new StringBuilder();
foreach (var artist in _artists)
{
sb.Append(artist);
sb.Append(", ");
}
if (sb.Length > 0)
{
sb.Length -= 2; // 去掉最后一个逗号
}
return sb.ToString();
}
private TimeSpan _duration;
///
/// 时长
///
public TimeSpan Duration
{
get => _duration;
set
{
_duration = value;
DurationStr = GetDurationStr();
}
}
private string _durationStr = "";
///
/// 时长字符串
///
public string DurationStr
{
get => _durationStr;
set => _durationStr = value;
}
///
/// 获取时长字符串
///
///
public string GetDurationStr()
{
if (Duration.Hours > 0)
{
return $"{Duration:hh\\:mm\\:ss}";
}
else
{
return $"{Duration:mm\\:ss}";
}
}
private uint _year;
///
/// 发行年份
///
public uint Year
{
get => _year;
set
{
_year = value;
YearStr = value.ToString();
}
}
private string _yearStr = "";
///
/// 发行年份字符串
///
public string YearStr
{
get => _yearStr;
set
{
if (value == "0")
{
_yearStr = "";
}
else
{
_yearStr = value;
}
}
}
private BitmapImage? _cover;
///
/// 封面(可能为空)
///
public BitmapImage? Cover
{
get => _cover;
set => _cover = value;
}
private string[] _genre = [];
///
/// 流派数组
///
public string[] Genre
{
get => _genre;
set
{
_genre = value;
GenreStr = GetGenre();
}
}
private string _genreStr = "";
///
/// 流派字符串, 为空时返回"未知流派"
///
public string GenreStr
{
get => _genreStr;
set
{
_genreStr = value;
OnPropertyChanged(nameof(GenreStr));
}
}
///
/// 获取流派字符串
///
///
public string GetGenre()
{
if (_genre == null || _genre.Length == 0)
{
return "未知流派";
}
var sb = new StringBuilder();
foreach (var genre in _genre)
{
sb.Append(genre);
sb.Append(", ");
}
if (sb.Length > 0)
{
sb.Length -= 2;
}
return sb.ToString();
}
private DateTimeOffset _modifiedDate;
///
/// 修改日期
///
public DateTimeOffset ModifiedDate
{
get => _modifiedDate;
set => _modifiedDate = value;
}
///
/// 获取文本前景色
///
///
///
/// 如果是当前播放歌曲, 返回主题色, 如果不是, 根据当前主题返回黑色或白色
public SolidColorBrush GetTextForeground(DetailedMusicInfo currentMusic, ElementTheme elementTheme)
{
if (Path == currentMusic.Path)
{
return (SolidColorBrush)App.Current.Resources["AccentTextFillColorTertiaryBrush"];
}
if (elementTheme == ElementTheme.Dark)
{
return new SolidColorBrush(Microsoft.UI.Colors.White);
}
else
{
return new SolidColorBrush(Microsoft.UI.Colors.Black);
}
}
public BriefMusicInfo()
{
}
public BriefMusicInfo(string path)
{
try
{
var musicFile = TagLib.File.Create(path);
Path = path;
ModifiedDate = new FileInfo(path).LastWriteTime;
ItemType = System.IO.Path.GetExtension(path).ToLower();
Album = musicFile.Tag.Album ?? "未知专辑";
Title = string.IsNullOrEmpty(musicFile.Tag.Title) ? System.IO.Path.GetFileNameWithoutExtension(path) : musicFile.Tag.Title;
Artists = musicFile.Tag.AlbumArtists.Concat(musicFile.Tag.Performers).ToArray().Length != 0 ? [.. musicFile.Tag.AlbumArtists, .. musicFile.Tag.Performers] : ["未知艺术家"];
Year = musicFile.Tag.Year;
Genre = musicFile.Tag.Genres.Length != 0 ? [.. musicFile.Tag.Genres] : ["未知流派"];
if (musicFile.Tag.Pictures != null && musicFile.Tag.Pictures.Length != 0)
{
var coverBuffer = musicFile.Tag.Pictures[0].Data.Data;
using var stream = new MemoryStream(coverBuffer);
stream.Seek(0, SeekOrigin.Begin);
Cover = new BitmapImage
{
DecodePixelWidth = 600,
DecodePixelHeight = 600
};
Cover.SetSource(stream.AsRandomAccessStream());
}
Duration = musicFile.Properties.Duration;
}
catch (Exception)
{
Path = path;
ModifiedDate = new FileInfo(path).LastWriteTime;
ItemType = System.IO.Path.GetExtension(path).ToLower();
Album = "未知专辑";
Title = System.IO.Path.GetFileNameWithoutExtension(path);
Artists = ["未知艺术家"];
Genre = ["未知流派"];
Duration = TimeSpan.Zero;
}
}
}
public class DetailedMusicInfo : BriefMusicInfo
{
private string[] _albumArtists = [];
///
/// 专辑艺术家数组
///
public string[] AlbumArtists
{
get => _albumArtists;
set
{
if (value?.Length > 0)
{
var tempArtists = Array.Empty();
foreach (var i in value)
{
if (i.Contains('、'))
{
tempArtists = [.. tempArtists, .. i.Split('、')];
}
else if (i.Contains(','))
{
tempArtists = [.. tempArtists, .. i.Split(',')];
}
else if (i.Contains(','))
{
tempArtists = [.. tempArtists, .. i.Split(',')];
}
else if (i.Contains('|'))
{
tempArtists = [.. tempArtists, .. i.Split('|')];
}
else if (i.Contains('/'))
{
tempArtists = [.. tempArtists, .. i.Split('/')];
}
else
{
tempArtists = [.. tempArtists, .. new[] { i }];
}
}
_albumArtists = tempArtists.Distinct().ToArray();
}
else
{
_albumArtists = [];
}
}
}
///
/// 获取专辑艺术家字符串
///
///
public string GetAlbumArtists()
{
if (_albumArtists == null || _albumArtists.Length == 0)
{
return "";
}
var sb = new StringBuilder();
foreach (var artist in _albumArtists)
{
sb.Append(artist);
sb.Append(", ");
}
if (sb.Length > 0)
{
sb.Length -= 2; // 去掉最后一个逗号
}
return sb.ToString();
}
public string _artistAndAlbumStr = "";
///
/// 艺术家和专辑名字符串
///
public string ArtistAndAlbumStr
{
get => _artistAndAlbumStr;
set => _artistAndAlbumStr = value;
}
///
/// 获取艺术家和专辑名字符串
///
///
public string GetArtistAndAlbumStr()
{
var artistsStr = GetArtists();
if (string.IsNullOrEmpty(artistsStr))
{
return Album ?? "";
}
if (string.IsNullOrEmpty(Album))
{
return artistsStr;
}
return $"{artistsStr} • {Album}";
}
private int _bitRate;
///
/// 比特率
///
public int BitRate
{
get => _bitRate;
set => _bitRate = value;
}
private int _track;
///
/// 曲目
///
public int Track
{
get => _track;
set => _track = value;
}
private string _lyric = "";
///
/// 歌词
///
public string Lyric
{
get => _lyric;
set => _lyric = value;
}
public DetailedMusicInfo()
{
}
public DetailedMusicInfo(string path) : base(path)
{
try
{
var musicFile = TagLib.File.Create(path);
AlbumArtists = [.. musicFile.Tag.AlbumArtists];
ArtistAndAlbumStr = GetArtistAndAlbumStr();
Track = (int)musicFile.Tag.Track;
Lyric = musicFile.Tag.Lyrics ?? "";
BitRate = musicFile.Properties.AudioBitrate;
}
catch (Exception)
{
AlbumArtists = [];
Track = 0;
BitRate = 0;
}
}
}