Ejemplo n.º 1
0
        public void DemoBasicUsageForID3v2()
        {
            // Create a new ID3v2 tag. ID3v2 works mostly like ID3v1 from this
            // perspective, although there are a few enhancements we can use.
            ID3v2Tag v2Tag = new ID3v2Tag();

            // no length restrictions with v2
            v2Tag.Album = "An album with a really long name that wouldn't fit into a ID3v1 tag";
            v2Tag.Genre = "A genre of my own creation!";             // not restricted to pre-defined genres with v2

            // Write our new tag out to a file as ID3v2.3 (for example; v2.2
            // and v2.4 would work too, although ID3v2.4 support is scare out
            // in the real world)
            v2Tag.WriteTag("basic.tag", ID3Versions.V2_3);

            // Read the tag back from the file.
            v2Tag = null;
            if (!ID3v2Tag.HasTag("basic.tag"))
            {
                Console.WriteLine("Hmmmm....something didn't go right here.");
            }
            else
            {
                v2Tag = ID3v2Tag.ReadTag("basic.tag");

                // Some usage possibilities:
                Console.WriteLine("Album: " + v2Tag.Album);

                // Make a change and write it back out to the file.
                v2Tag.Comments = "New comments";
                v2Tag.WriteTag("basic.tag", ID3Versions.V2_3);

                // Show that the change worked.
                v2Tag = ID3v2Tag.ReadTag("basic.tag");
                if (v2Tag.HasComments)
                {
                    Console.WriteLine("The comments we just added: " + v2Tag.Comments);
                }
                else
                {
                    Console.WriteLine("Hmmmm....something didn't go right here.");
                }

                // Some other stuff:
                ID3Versions version = ID3v2Tag.LookForTag("basic.tag");
                Console.WriteLine("ID3 tag found, version: " + version.ToString());
            }
        }
Ejemplo n.º 2
0
        private void ReadTagsFromFile()
        {
            if (Status != ItemStatus.Complete)
            {
                return;
            }

            if (!CompleteDestination.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase))
            {
                return;
            }

            try
            {
                ID3Tag tag = ID3v2Tag.ReadTag(CompleteDestination);

                if (tag == null)
                {
                    tag = ID3v1Tag.ReadTag(CompleteDestination);
                    if (tag == null)
                    {
                        tag = new ID3v2Tag();
                    }
                }

                TrackTitleTag = tag.Title;
                AlbumTag      = tag.Album;
                ArtistTag     = tag.Artist;
                GenreTag      = tag.Genre;

                _tagsSet = true;
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error: unable to read ID3 Tag for file: " + CompleteDestination + ". " + ex.Message);
            }
        }
Ejemplo n.º 3
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.
        }
Ejemplo n.º 4
0
        public bool WriteTagsToFile(bool overwriteExistingTags)
        {
            if (Status != ItemStatus.Complete)
            {
                return(false);
            }

            if (!CompleteDestination.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase))
            {
                return(false);
            }

            try
            {
                ID3Tag tag = null;

                switch (ID3v2Tag.LookForTag(CompleteDestination))
                {
                case ID3Versions.None:
                case ID3Versions.Unknown:
                default:

                    tag = new ID3v2Tag();
                    SetTagContent(tag, overwriteExistingTags);
                    tag.WriteTag(CompleteDestination, ID3Versions.V2_4);

                    break;

                case ID3Versions.V1:
                case ID3Versions.V1_0:
                case ID3Versions.V1_1:

                    tag = ID3v1Tag.ReadTag(CompleteDestination);
                    SetTagContent(tag, overwriteExistingTags);
                    tag.WriteTag(CompleteDestination, ID3Versions.V1_1);

                    break;

                case ID3Versions.V2:
                case ID3Versions.V2_2:
                case ID3Versions.V2_3:

                    tag = ID3v2Tag.ReadTag(CompleteDestination);
                    SetTagContent(tag, overwriteExistingTags);
                    tag.WriteTag(CompleteDestination, ID3Versions.V2_3);

                    break;

                case ID3Versions.V2_4:

                    tag = ID3v2Tag.ReadTag(CompleteDestination);
                    SetTagContent(tag, overwriteExistingTags);
                    tag.WriteTag(CompleteDestination, ID3Versions.V2_4);

                    break;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error writing tags to: " + CompleteDestination + ". " + ex.Message);

                return(false);
            }

            return(true);
        }