Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / MFC
Article

CMp3Tags - id3v1.1 Tag Reader/Writer

Rate me:
Please Sign up or sign in to vote.
4.30/5 (12 votes)
3 Feb 2005Public Domain2 min read 74.9K   2.6K   41   12
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, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer Iglu.com
United Kingdom United Kingdom
Been programming for nearly 8 years now, 3 years professionally. Now working for Iglu.com (one of the developers for www.IgluSki.com)

Check out my blog for more code related articles!

Comments and Discussions

 
QuestionHow to read artwork album image from MP3 file Pin
Surendrai3-Nov-09 7:32
Surendrai3-Nov-09 7:32 
GeneralLittle improvent: if 'TAG' isn't there. Pin
Xanblax1-Sep-08 1:31
Xanblax1-Sep-08 1:31 
GeneralRe: Little improvent: if 'TAG' isn't there. Pin
Dean Thomas20-Dec-08 0:43
Dean Thomas20-Dec-08 0:43 
Generalusing on eVC++ Pin
kimhoontae15-Nov-07 20:43
kimhoontae15-Nov-07 20:43 
GeneralUpdated code (Unicode) Pin
jimwillsher30-Jun-06 0:19
jimwillsher30-Jun-06 0:19 
GeneralWrite a tag not work Pin
5il3nt5-Feb-06 12:49
5il3nt5-Feb-06 12:49 
GeneralMemory leaks Pin
yuraka3-Oct-05 2:02
yuraka3-Oct-05 2:02 
Generalbug in code Pin
deek1b25-Mar-05 23:21
deek1b25-Mar-05 23:21 
GeneralSome Comments Pin
Johann Gerell5-Feb-05 3:36
Johann Gerell5-Feb-05 3:36 
GeneralRe: Some Comments Pin
Dean Thomas5-Feb-05 4:56
Dean Thomas5-Feb-05 4:56 
QuestionWhat about ID3v2 tags? Pin
julcho4-Feb-05 0:24
julcho4-Feb-05 0:24 
AnswerRe: What about ID3v2 tags? Pin
Dean Thomas4-Feb-05 0:26
Dean Thomas4-Feb-05 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.