コード例 #1
0
ファイル: playlist.cs プロジェクト: devorchik/plcopy
    // 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);
    }