/// <summary> /// Maps the Taglib file to the required MusicFileInfo class. /// </summary> /// <param name="taggedFile">Teh tagged taglib file</param> /// <returns>A MusicFileInfo class</returns> /// <remarks> /// This copy kept as a private routing within this class as /// it is not used anywhere else within the application and is /// specific to this MusicFileService. /// </remarks> public static MusicFileInfo MapTaglibFileToMusicFileInfo(TagLib.File taggedFile) { var musicFile = new MusicFileInfo(); // Track if (taggedFile.Tag.Title != null) musicFile.TrackName = taggedFile.Tag.Title; //if (taggedFile.Tag.Track != null) musicFile.TrackNumber = Convert.ToInt32(taggedFile.Tag.Track); if (taggedFile.Properties.Duration != null) musicFile.TrackDuration = taggedFile.Properties.Duration; if (taggedFile.Tag.MusicBrainzTrackId != null) musicFile.TrackMbid = taggedFile.Tag.MusicBrainzTrackId; // Artist if (taggedFile.Tag.AlbumArtists.Count() > 0) musicFile.ArtistName = taggedFile.Tag.AlbumArtists[0]; else { if (taggedFile.Tag.Performers.Count() > 0) musicFile.ArtistName = taggedFile.Tag.Performers[0]; else { // The Artists is marked as obsolete, use it in the // event an old file is being processed and the // up to date information is not available. // Shouldn't get here really. if (taggedFile.Tag.Artists.Count() > 0) musicFile.ArtistName = taggedFile.Tag.Artists[0]; else musicFile.ArtistName = "No Artist Found for Media file"; } } if (taggedFile.Tag.MusicBrainzArtistId != null) musicFile.ArtistMbid = taggedFile.Tag.MusicBrainzArtistId; // Genres if (taggedFile.Tag.Genres.Count() > 0) { foreach (var tag in taggedFile.Tag.Genres) { musicFile.Genres.Add(tag); } } // Album information if (taggedFile.Tag.Album != null) musicFile.AlbumTitle = taggedFile.Tag.Album; //if (taggedFile.Tag.Year != null) musicFile.AlbumReleased = new DateTime(Convert.ToInt32(taggedFile.Tag.Year), 1, 1); if (taggedFile.Tag.MusicBrainzDiscId != null) musicFile.AlbumMbid = taggedFile.Tag.MusicBrainzDiscId; return musicFile; }
/// <summary> /// Gets the music info from the supplied filename and subfolder name. /// The main folder is defaulted to the User's Music Library as a /// Known folder /// </summary> /// <param name="musicFile">The instance of the music file to be read.</param> /// <returns>An instance of the music file, includeing Tag information</returns> public static async Task<TagLib.File> GetMusicFileInfoAsync(StorageFile musicFile) { MusicFileInfo musicFileInformation = new MusicFileInfo(); // Use Taglib Sharp to get the tag information. // 1. Create a file abstraction to avoid using banned libraries // http://stackoverflow.com/questions/13381494/c-sharp-windows-store-app-granting-capabilities-to-used-dll-s // Explanation is as follows // You can use TagLibSharp to load tags by creating a StreamFileAbstraction and passing that to File.Create. // This won't use any banned APIs. // // The StreamFileAbstraction class is defined below. // I claim no credit for this, just that it works nicely. The resource is disposed of // as the class inherits the Taglib.file.IFileAbstraction class. IRandomAccessStreamWithContentType f = await musicFile.OpenReadAsync(); TagLib.File taglibMusicFile = TagLib.File.Create(new StreamFileAbstraction(musicFile.Name, f.AsStream())); return taglibMusicFile; }
/// <summary> /// Maps the MusicFileInfo to an Album model of the Domain. /// </summary> /// <param name="MusicFile">The imported music file information</param> /// <returns>The mapped Album class</returns> private Album MapMusicFileInfoToAlbum(MusicFileInfo MusicFile, string filePath) { var newAlbum = _albumFactory.Create(); // Do the mapping here. newAlbum.Title = MusicFile.AlbumTitle; newAlbum.Released = MusicFile.AlbumReleased; newAlbum.Mbid = MusicFile.AlbumMbid; var newArtist = _artistFactory.Create(); newArtist.Name = MusicFile.ArtistName; newArtist.Mbid = MusicFile.ArtistMbid; newAlbum.Artist = newArtist; newAlbum.Tracks = new List<Track>(); var newTrack = _trackFactory.Create(); newTrack.Number = MusicFile.TrackNumber; newTrack.Title = MusicFile.TrackName; newTrack.Duration = MusicFile.TrackDuration; newTrack.Mbid = MusicFile.TrackMbid; newTrack.Artist = newArtist; newTrack.mediaFilePath = filePath; newAlbum.Tracks.Add(newTrack); foreach (var genre in MusicFile.Genres) { var newGenre = _genreFactory.Create(); newGenre.Name = genre; newAlbum.Genres.Add(newGenre); } return newAlbum; }