示例#1
0
        public void DemoBasicUsageForID3v1()
        {
            // Create a new ID3v1 tag.
            ID3v1Tag v1Tag = new ID3v1Tag();

            v1Tag.Album       = "My Track Album";
            v1Tag.Artist      = "My Track Artist";
            v1Tag.Comments    = "My Track Comments";
            v1Tag.Genre       = "Rock";       // this has to be a genre from the ID3v1 genre list (see ID3Genre.cs)
            v1Tag.Title       = "My Track Title";
            v1Tag.TrackNumber = 1;
            v1Tag.Year        = "2004";

            // Write our new tag out to a file as ID3v1.1 (for example; v1.0 would work too)
            v1Tag.WriteTag("basic.tag", ID3Versions.V1_1);

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

                // Some usage possibilities:
                Console.WriteLine("Album: " + v1Tag.Album);
                if (v1Tag.HasArtist)                   // checks to see if Artist isn't null or empty
                {
                    Console.WriteLine("Artist: " + v1Tag.Artist);
                }

                // Make a change and write it back out to the file.
                v1Tag.Comments = null;
                v1Tag.WriteTag("basic.tag", ID3Versions.V1_1);

                // Show that the change worked.
                v1Tag = ID3v1Tag.ReadTag("basic.tag");
                if (v1Tag.HasComments)
                {
                    Console.WriteLine("Hmmmm....something didn't go right here.");
                }
                else
                {
                    Console.WriteLine("See! This library really does work.");
                }
            }
        }
示例#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);
            }
        }
示例#3
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);
        }