As promised, ID3v1 Mp3 Tag Reader

Post title is pretty much self-explanatory, I hope you enjoy it, I will see if i can sit through adding comments and cleaning up code, Enjoy!!! :)

myTaglib.cs:


using System;
using System.IO;
using System.Text;
using myTaglibInfo;

namespace myTaglib
{
    class TagRead
    {
        public TagList ReadTag(string FilePath){
           
            // Grabs List of Variables dedicated to ID3 Tags
            TagList tag = new TagList();
       
            // Declare "rawData" <- take in binary and store 128bytes
            byte[] rawData = ReadFromFile(FilePath);

            // Convert the binary or UTF8 code to String
            string binaryData = Encoding.UTF8.GetString(rawData);

            if (binaryData.Length >= 128)
            {
                // Stores Tags in there appropriate
                // corresponding variables
                tag.Header = binaryData.Substring(0, 3);
                tag.TrackTitle = binaryData.Substring(3, 30);
                tag.ArtistName = binaryData.Substring(33, 30);
                tag.AlbumName = binaryData.Substring(63, 30);
                tag.Year = binaryData.Substring(93, 4);
                tag.Comments = binaryData.Substring(97, 30);
                int Zero = 0;
                int.TryParse(binaryData.Substring(125, 1), out Zero);
                tag.ZeroByte = Zero;
                int Track = 0;
                int.TryParse(binaryData.Substring(125, 1), out Track);
                tag.TrackNumber = Track;
                int Genre = 0;
                int.TryParse(binaryData.Substring(127, 1), out Genre);
                tag.Genre = Genre;
            }
            return tag;
        }
        private byte[] ReadFromFile(string FilePath)
        {
            // Declare File and Binary
            FileStream file = null;
            BinaryReader inputData = null;
            byte[] outResult = null;

            try
            {
                // Flag filestream to be opened and read
                // BinaryReader obviously reads the data taken in
                file = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                inputData = new BinaryReader(file);

                // Set positioning for last 128 bytes of the MP3
                inputData.BaseStream.Position = file.Length - 128;
                outResult = inputData.ReadBytes((int)file.Length);

            }
            finally
            {
                if (inputData != null)
                {
                    inputData.Close();
                }
                if (file != null)
                {
                    file.Close();
                    file.Dispose();
                }
            }
            // Send recieve data from file
            return outResult;

        }
    }
}

TagList.cs



using System;
using System.Text;

namespace myTaglibInfo
{
    class TagList
    {
        public string Header;
        public string TrackTitle;
        public string ArtistName;
        public string AlbumName;
        public string Year;
        public string Comments;
        public int ZeroByte;
        public int TrackNumber;
        public int Genre;
    }
}

Main.cs

using System;
using System.Text;
using myTaglib;
using myTaglibInfo;
using MyForm;

namespace RunNow
{
    class Base 
    {
        public static void Main ()
        {
            string FilePath = String.Empty;

            Form1 userInput = new Form1();
            
           // Calls our Method
            TagRead reader = new TagRead();
            TagList tag = reader.ReadTag(FilePath);

           // Gives us a final result of our ID3v1 Tag's outputted
           // This is just to test the module
            Console.WriteLine("\n\n\nTrack Name:\t{0}\nArtist Name:\t{1}\nAlbum Name:\t{2}\nYear Made:\t{3}", tag.TrackTitle, tag.ArtistName, tag.AlbumName, tag.Year);
            Console.Read();
        }
    }
}



Comments

Popular Posts