Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CMp3Tags - id3v1.1 Tag Reader/Writer

0.00/5 (No votes)
3 Feb 2005 1  
An article on reading and writing tags for MP3 files.

Sample Image

Introduction

After you've read through this article, you will see how easy it can be to read and write ID3 tags for MP3 files!

I've written a little class to allow users to incorporate MP3 tags into their software, and it's very simple to use.

Background

I needed an application to use ID3 tags for re-naming MP3 files and sorting them etc., so I wrote my own class as all of the others I came across were too big for just what I needed. Basic info on ID3 can be found here.

Using the code

To use CMp3Tags, first you need to include Mp3Tags.h into one of your source files. Most of you will know how to do this, but for those of you who don't, I'm going to take you right through how to do it!

#include "CMp3Tags.h"

Now that we've included the file into our project, we'll need to find a MP3 to use. To save on time, just find a specific MP3 you want to use, make a copy of it, and place it in the root of your C drive, e.g., C:\mp3test.mp3.

CMp3Tags Tags;
Tags.OpenFile("C:\\mp3test.mp3");

This will create a instance of CMp3Tags and open the file we've just put in C:\. When we open a file with this class, it automatically reads the Id3 tags in, so it saves us some time in calling all sorts of different functions to get certain data. When it reads the data, it stores it in these private members of the class:

CString m_strSongTitle;
CString m_strArtist;
CString m_strAlbum;
CString m_strYear;
CString m_strComment;

Because these are private members, we can't access these from outside the class, so we use functions to return these strings to us for use in our applications.

CMp3Tags Tags;
Tags.OpenFile("C:\\mp3test.mp3");

//New Code

CString strTitle = Tags.GetSongTitle();

That new line of code we just added will return the MP3's title to us. However, if the MP3 doesn't contain a title in the tags, it will return nothing. So if we wanted to, we could do a little check, to see if it returned anything, and if it doesn't, we put our own title in it!

And there:

CMp3Tags Tags;
Tags.OpenFile("C:\\mp3test.mp3");

CString strTitle = Tags.GetSongTitle();   

//New Code

if ( strTitle.IsEmpty() )
{
     Tags.SetSongTitle("CMp3Tags - Remix");
}
 
Tags.CloseFile();

we have it, basically that's it!

Here is a list of functions that will set/get variables!

//Set Variables

int SetSongTitle(LPCTSTR lpSongName);
int SetArtist(LPCTSTR lpArtist);
int SetAlbum(LPCTSTR lpAlbum);
int SetYear(LPCTSTR lpYear);
int SetComment(LPCTSTR lpComment);
 
//Get Variables

CString GetSongTitle();
CString GetArtist();
CString GetAlbum();
CString GetYear();
CString GetComment();

All functions with an int return type will return < 0 on error, and 0 on success.

Well, that's about it folks! Hope you enjoyed my first article on CodeProject. Hopefully, I'll be writing some more :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here