// parse the iTunes library and read the track data for a given list, returning a reference to the TrackCollection public bool LoadiTunesDataFile(string strDataFile, string strPlaylist, out TrackCollection tcOut) { tcOut = null; // don't assume this was initialized PropertyList plistRoot = new PropertyList(); plistRoot.ParseFile(strDataFile); foreach (PropertyList plistPlaylist in plistRoot.Value("Playlists")) { if (plistPlaylist.Value("Name") == strPlaylist) { tcOut = new TrackCollection() { Name = plistPlaylist.Value("Name"), PersistentKey = plistPlaylist.Value("Playlist Persistent ID"), }; if (plistPlaylist.Contains("Playlist Items")) { // itterate over all the items in the playlist and dereference the track information to get metadata on the song // note - iTunes XML only contains those fields in the original file so we must handle the "optional" data... foreach (PropertyList plistItem in plistPlaylist.Value("Playlist Items")) { PropertyList plistTrack = plistRoot.Value("Tracks").Value(string.Format("{0}", plistItem.Value("Track ID"))); if (plistTrack.Value("Track Type") == "File") { string strFile = _UriToFilePath(plistTrack.Value("Location")); TrackReference tr = new TrackReference() { SourcePath = strFile, Name = plistTrack.Value("Name"), Size = plistTrack.Value("Size"), Artist = plistTrack.Value("Artist", "Unknown Artist"), Album = plistTrack.Value("Album", "Unknown Album"), Compilation = plistTrack.Value("Compilation", false), DiscNumber = plistTrack.Value("Disc Number", 1), TrackNumber = plistTrack.Value("Track Number", 1), Duration = plistTrack.Value("Total Time", 0), }; tcOut.Tracks.Add(tr); } } } break; } } return (tcOut != null); }
public override int GetHashCode() { return(TrackReference.GetHashCode()); }
// "block" copy a track from the source to destination allowing for progress callbacks. private void _CopyFile(TrackReference tr, string strDest) { try { using (FileStream streamIn = File.OpenRead(tr.SourcePath)) { try { using (FileStream streamOut = File.Create(strDest)) { int cbOffset = 0; while (streamIn.Length - cbOffset > 0) { int cbBlockSize = Math.Min((int)streamIn.Length - cbOffset, 2 * 1024 * 1024); // copy in 2MB blocks byte[] buffer = new byte[cbBlockSize]; streamOut.Write(buffer, 0, streamIn.Read(buffer, 0, cbBlockSize)); cbOffset += cbBlockSize; } streamOut.Close(); } streamIn.Close(); } catch (IOException) { throw new CopyException(CopyException.Failure.FailedToCreateDestinationFile, "Failed to create: " + strDest); } } } catch (IOException) { throw new CopyException(CopyException.Failure.FailedToOpenSourceTrack, "Failed to open: " + tr.SourcePath); } }
static void _CopyProgressHandler(TrackReference tr) { Console.WriteLine(String.Format("Copying: {0}", tr.Name)); }