Click here to Skip to main content
6,596,602 members and growing! (20,824 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate License: A Public Domain dedication

CMp3Tags - id3v1.1 Tag Reader/Writer

By Dean Thomas

An article on reading and writing tags for MP3 files.
VC6, VC7, VC7.1, VC8.0, Windows, MFC, VS.NET2003, Dev
Posted:3 Feb 2005
Views:41,380
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.17 Rating: 4.17 out of 5
1 vote, 10.0%
1

2
2 votes, 20.0%
3
2 votes, 20.0%
4
5 votes, 50.0%
5

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

About the Author

Dean Thomas


Member
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!
Occupation: Software Developer
Company: Iglu.com
Location: United Kingdom United Kingdom

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralHow to read artwork album image from MP3 file PinmemberSurendrai8:32 3 Nov '09  
GeneralLittle improvent: if 'TAG' isn't there. PinmemberXanblax2:31 1 Sep '08  
GeneralRe: Little improvent: if 'TAG' isn't there. PinmemberDean Thomas1:43 20 Dec '08  
Generalusing on eVC++ Pinmemberkimhoontae21:43 15 Nov '07  
GeneralUpdated code (Unicode) Pinmemberjimwillsher1:19 30 Jun '06  
GeneralWrite a tag not work Pinmember5il3nt13:49 5 Feb '06  
GeneralMemory leaks Pinsussyuraka3:02 3 Oct '05  
Generalbug in code Pinmemberdeek1b0:21 26 Mar '05  
GeneralSome Comments PinmemberJohann Gerell4:36 5 Feb '05  
GeneralRe: Some Comments PinmemberDean19885:56 5 Feb '05  
GeneralWhat about ID3v2 tags? Pinmemberjulcho1:24 4 Feb '05  
GeneralRe: What about ID3v2 tags? PinmemberDean19881:26 4 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Feb 2005
Editor: Smitha Vijayan
Copyright 2005 by Dean Thomas
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project