Id3TAG and C#

The other day one of my friends told me that he was tired of all the programs on the marked claiming that they were perfect for keeping track of his music collection. When I asked what he was looking for, he told me that he needed a program that could read in his collection of mp3’s and keep track of these music files. Thay should be loaded into the program, and listed by their Tag id (title, artist, album and so on). You should also be able to edit the tags, mark all mp3’s and select ”remove all comments”. He also wanted the program to able to get titles, album names and so on for the files in his collection which doesn’t have a tag. Now this last part is properly the hardest thing (not impossible) to accomplish, I decided to write a blog on how to make such a program.

In this first part, I started out with trying to retrieve the information, and putting the output on the console (This will be in version ID3v1, but later on it will also be in version ID3v2). I mean, test the functionality before deciding on GUI layout and all. This meant that I have to somehow search through my ”music” directory and list all files, and subdirectories files. And what better way to do that than with a recursive method. Now here is what I did.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace Id3

{

    class test

    {

        public byte[] TAGID = new byte[3];     

        public byte[] Title = new byte[30];     

        public byte[] Artist = new byte[30];    

        public byte[] Album = new byte[30];     

        public byte[] Year = new byte[4];       

        public byte[] Comment = new byte[30];   

        public byte[] Genre = new byte[1];      

        //Search through directories and sub directories to find mp3's

        //and I do this by a recursive method..

        public void SearchDirectories(string StartPath)

        {

            string[] dirs = Directory.GetDirectories(StartPath);

            if (dirs.Length > 0)

            {

                foreach (string subDirectories in dirs)

                {

                    SearchDirectories(subDirectories);

                }                           

            }

          

            //Will reach here after the current directory has been checked for other directories..

            string[] fileNames = Directory.GetFiles(StartPath);

            foreach (string fileName in fileNames)

            {   // When utilizing "using" we get the benefit of not having to close the FileStream ourselves, this is done for us. (there is more, but I will elaborate on this later).

                using (System.IO.FileStream fs = System.IO.File.OpenRead(fileName))

                {   // The tag information is the last 128 bit of an mp3...

                    if (fs.Length >= 128)

                    {

                        test tag = new test();

                        fs.Seek(-128, System.IO.SeekOrigin.End);

                        fs.Read(tag.TAGID, 0, tag.TAGID.Length);

                        fs.Read(tag.Title, 0, tag.Title.Length);

                        fs.Read(tag.Artist, 0, tag.Artist.Length);

                        fs.Read(tag.Album, 0, tag.Album.Length);

                        fs.Read(tag.Year, 0, tag.Year.Length);

                        fs.Read(tag.Comment, 0, tag.Comment.Length);

                        fs.Read(tag.Genre, 0, tag.Genre.Length);

                        string theTAGID = Encoding.Default.GetString(tag.TAGID);

                        if (theTAGID.Equals("TAG"))

                        {

                            string Title = Encoding.Default.GetString(tag.Title);

                            string Artist = Encoding.Default.GetString(tag.Artist);

                            string Album = Encoding.Default.GetString(tag.Album);

                            string Year = Encoding.Default.GetString(tag.Year);

                            string Comment = Encoding.Default.GetString(tag.Comment);

                            string Genre = Encoding.Default.GetString(tag.Genre);

                            Console.WriteLine("Title: {0}", Title);

                            Console.WriteLine("Artist: {0}", Artist);

                            Console.WriteLine("Album: {0}", Album);

                            Console.WriteLine("Year: {0}", Year);

                            Console.WriteLine(Comment);

                            Console.WriteLine("Genre: {0}", Genre);

                            Console.WriteLine();

                        }

                    }

                }

            } Console.ReadLine();

        }

    }

}

This is actually pretty strait forward, there is nothing difficult about this piece of code. I did not have to use a recursive method in order to achieve this functionality, I did this because I recently finished a course in algorithms, and I’m simply testing my abilities.

I will continue in the next part, I have not decided what functionality to implement next (I think it will be focused to getting tag information out of the ID3v2 tags, which are quite different!), but I will return. If you find that I don’t explain my code enough, please email me, and I will make it better. I’m trying to find out who my audience is, and at what level of experience you are, so that I can make it just right. So don’t hesitate to comment.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: AllanJP
Posted on: 11/4/2008 at 1:55 AM
Tags: ,
Categories: C#
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (6) | Post RSSRSS comment feed