Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
Your first MP3 Delphi player
Part 4: MP3 ID3 Tag Editing
 More of this Feature
• Part 1: Delphi plays MP3
• Part 2: mp3Player GUI
• Part 3: mp3Player Code
• Part 5: Project's Code
 Join the Discussion
"Hi, does anybody know a fast mp3 encoder like in the xing-products, I could use in Delphi Programms?"
Fast Mp3 Encoder
  Related Resources
• Multimedia VCL/Tools
• Records in Object Pascal
• File I/O with Delphi
• BrowseForFolder dialog
 From Other Guides
• About MP3/MIDI Music
 Elsewhere on the Web
• Information about audio tagging such as ID3
 

The thing that makes MP3 files even more great are ID3 tags. Every MP3 file has a little blurb of data on it, which is used to give informations about artist, title, album, publishing year and genre about a song. The "ID3" Tag is attached to most MP3 files these days. The tag is always 128 bytes long and is located at very end of the MP3-file.

The ID3 tag is saved in the last 128 bytes of a MP3 file. ID3 tag should start with a "TAG" string. If this string is absent that means that ID3 information has been removed - or was never set. But don't worry - all we have to do is append it to the file.

The ID3-Tag structure can be described as follows:

type
 TID3Rec = packed record
  Tag     : array[0..2] of Char;
  Title,
  Artist,
  Comment,
  Album   : array[0..29] of Char;
  Year    : array[0..3] of Char;
  Genre   : Byte;
end;

Note that genres are determined by one byte of data only. This byte has to be translated into text from the following declaration:

const
  MaxID3Genre=147;

  ID3Genre: array[0..MaxID3Genre] of string = (
    'Blues', 'Classic Rock', 'Country', 'Dance', 
    ...
    'Synthpop'  {and probably more to come}
  );

Read ID3
To read the ID3 tag from a MP3 song we have to go down to file system and use IO routines. Here goes the code to read the ID3 information:

procedure FillID3TagInformation
(mp3File: string;
Title,Artist,Album,Year,Genre,Comment:TEdit);
var ID3 : TID3Rec;
    fmp3: TFileStream;
begin
  fmp3:=TFileStream.Create(mp3File, fmOpenRead);
  try
    fmp3.position:=fmp3.size-128;
    fmp3.Read(ID3,SizeOf(ID3));
  finally
    fmp3.free;
  end;

 if ID3.Tag <> 'TAG' then begin
   Title.Text:='Wrong or no ID3 tag information';
   Artist.Text:='Wrong or no ID3 tag information';
   Album.Text:='Wrong or no ID3 tag information';
   Year.Text:='Wrong or no ID3 tag information';
   Genre.Text:='Wrong or no ID3 tag information';
   Comment.Text:='Wrong or no ID3 tag information';
 end else begin
   Title.Text:=ID3.Title;
   Artist.Text:=ID3.Artist;
   Album.Text:=ID3.Album;
   Year.Text:=ID3.Year;
   if ID3.Genre in [0..MaxID3Genre] then
     Genre.Text:=ID3Genre[ID3.Genre]
   else
     Genre.Text:=IntToStr(ID3.Genre);
   Comment.Text:=ID3.Comment
 end;
end;

This code uses the TFileStream to access the information in MP3 file. Once we have successfully opened a Mp3 file set the position to EndOfFile-128 bytes and read the ID3 tag. The rest of the code in this procedure simply checks whether there is an ID3 tag information in a file and fills those Edit boxes (parameters in this procedure) with values.

Write ID3
The writing part is simple too. This is the function declaration, the whole code can be found in the Project's code part of this article.

procedure ChangeID3Tag (NewID3: TID3Rec; mp3FileName: string);

Note that in the Project's code the call to this function is not covered. This is something I'll leave to you to implement.

Before the end
Finally, we've made it! You have your own ready-to-play-and-get-some-money MP3 player working. What I want do to here is to suggest to "play" with it a little and modify it a lot. How about adding some kind of skin to this player in the means of custom shaped forms. There are certainly some great ideas you can compose in this project, let the other see what you have made: post you creation to the Free Applications section on this site.

Next page > MP3 Delphi player Project's Code > Page 1, 2, 3, 4, 5

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming