aurora/Aurora/Backend/Services/LibraryService.cs
2019-05-22 18:59:15 -04:00

65 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Aurora.Backend.Models;
using Aurora.Backend.Utils;
namespace Aurora.Backend.Services
{
public class LibraryService : BaseService<LibraryService>
{
#region Fields
private string _pathName = "/Users/brandonwatson/Music/iTunes/iTunes Media/Music";
private string _extensions = ".wav,.mp3,.aiff,.flac,.m4a,.m4b,.wma";
private Dictionary<string, BaseSong> _library;
#endregion Fields
public LibraryService()
{
_library = new Dictionary<string, BaseSong>();
LoadLibrary();
}
/// <summary>
/// Gets the songs.
/// </summary>
/// <returns>The songs.</returns>
public ObservableCollection<BaseSong> GetLibrary()
{
ObservableCollection<BaseSong> collection = new ObservableCollection<BaseSong>();
foreach (KeyValuePair<string, BaseSong> pair in _library)
{
collection.Add(pair.Value);
}
return collection;
}
/// <summary>
/// Loads library from files.
/// </summary>
private void LoadLibrary()
{
//Get songs
List<FileInfo> musicFiles = FileSystemUtils.TraverseFoldersAsync(_pathName, _extensions);
foreach (FileInfo file in musicFiles)
{
TagLib.File tagFile = TagLib.File.Create(file.FullName);
BaseSong song = new LocalSong(file)
{
Title = tagFile.Tag.Title,
Album = tagFile.Tag.Album,
Artist = tagFile.Tag.FirstAlbumArtist
};
_library.Add(song.Id, song);
}
}
}
}