Ejemplo n.º 1
0
        public void DemoAdvancedUsage()
        {
            // ID3v2 Track Number frames allow you to specify not only the track number,
            // but the total number of tracks as well. Use a TRCKFrame to get to this
            // functionality. (The frame classes are named after their ID3v2.4 frame IDs,
            // e.g. TRCK for Track Number and PRIV for Private Use.)
            ID3v2Tag tag = new ID3v2Tag();

            // This somewhat awkward method for creating frames does have some benefits
            // in other areas.
            TRCKFrame trackNumberFrame = (TRCKFrame)ID3v2Frame.GetNewFrame(FrameType.TrackNumber);

            // Add the track number data.
            trackNumberFrame.TrackNumber = 1;
            trackNumberFrame.TotalTracks = 10;

            // Add the frame to the tag.
            tag.AddFrame(trackNumberFrame);

            // Most of the usual frames are simple implemented as TextInformationFrame's.
            TextInformationFrame artistFrame = (TextInformationFrame)ID3v2Frame.GetNewFrame(FrameType.LeadArtist);

            artistFrame.Text = "My Artist";
            tag.AddFrame(artistFrame);

            // Now that the artist frame is there, the Artist property will work
            Console.WriteLine("Artist: " + tag.Artist);
            // but the others still don't.
            if (tag.HasAlbum)
            {
                Console.WriteLine("Hmmmm....something didn't go right here.");
            }
            else
            {
                Console.WriteLine("Album (still null): " + tag.Album);
            }
            // While we're on the topic of the properties, TrackNumber works, but only returns
            // the track number, not the total tracks (can't fit both into one int, of course)
            Console.WriteLine("Track Number: " + tag.TrackNumber);

            // Use a Private frame (PRIVFrame) to hold some private data
            PRIVFrame privateFrame = (PRIVFrame)ID3v2Frame.GetNewFrame(FrameType.Private);

            privateFrame.OwnerIdentifier = "ID3Sharp demo";
            privateFrame.PrivateData     = new byte[] { 0x49, 0x44, 0x33, 0x53, 0x68, 0x61, 0x72, 0x70 };
            tag.AddFrame(privateFrame);

            // Write the tag out to a file
            tag.WriteTag("advanced.tag", ID3Versions.V2_3);
            // and read it back.
            tag = ID3v2Tag.ReadTag("advanced.tag");

            // Verify the contents
            privateFrame = (PRIVFrame)tag[FrameType.Private];
            Console.Write("Private Data: ");
            foreach (byte dataByte in privateFrame.PrivateData)
            {
                Console.Write(dataByte);
                Console.Write(" (" + (char)dataByte + ") ");
            }
            Console.WriteLine();

            // Remove the frames, with a few (for demonstration) different methods
            tag.RemoveFrame(privateFrame);
            tag.RemoveFrame(FrameType.LeadArtist);
            tag[FrameType.TrackNumber] = null;

            tag.WriteTag("empty.tag", ID3Versions.V2_3);

            // Questions? Problems? Suggestions? Post to a forum on the project site
            // or send me an email.
        }