Read an Mp3s Files Information

The ID3 format used by Mp3 is considerably more complicated than other metadata formats, so you might need want to use the low level interface described as well as the common interface. An Mp3 can contain both a ID3v1 tag and an ID3v2 tag, both tags are optional. The ID3v1 tag can be version 1.0 or version 1.1. An ID3v2 tag can be version 2.2, 2.3 or 2.4 and the differences between the versions is quite considerable.

The stuff you might want to know about but you can't change is stored in the AudioHeader class


MP3File f      = (Mp3File)AudioFileIO.read(testFile);
MP3AudioHeader audioHeader = f.getAudioHeader();
audioHeader.getTrackLength();
audioHeader.getSampleRateAsNumber();
mp3AudioHeader.getChannels();
mp3AudioHeader.isVariableBitRate();

Mp3 provides a subclass called MP3AudioHeader that contains additional information.


mp3AudioHeader.getTrackLengthAsString();
mp3AudioHeader.getMpegVersion();
mp3AudioHeader.getMpegLayer();
mp3AudioHeader.isOriginal();
mp3AudioHeader.isCopyrighted();
mp3AudioHeader.isPrivate();
mp3AudioHeader.isProtected();
mp3AudioHeader.getBitRate();
mp3AudioHeader.getEncodingType();

The metadata information that you can change is stored in tags. The getTag() method only returns the ID3v1 Tag , this is probably the tag you are least interested in. To get the ID3v2 tag you can use the getID3v2Tag() method. Alternatively Jaudiotagger provides a getID3v2TagAsv24() method, this returns the v2Tag converted to v24 format if it was not already in v24 format, the advantage of this is that you can treat all Mp3 files as if their v2 tag is in v24 format whether or not it actually is.


MP3File f      = (Mp3File)AudioFileIO.read(testFile);
Tag tag        = f.getTag();
ID3v1Tag         v1Tag  = (ID3v1Tag)tag;
AbstractID3v2Tag v2tag  = f.getID3v2Tag()
ID3v24Tag        v24tag = (AbstractID3v2Tag)f.getID3v2TagAsv24();

The v1 tag only contains a very limited set of fields, these can be read as follows

  
(tag.getFirstArtist());
(tag.getFirstAlbum());
(tag.getFirstTitle());
(tag.getFirstComment());
(tag.getFirstYear());
(tag.getFirstTrack());	

The rest of this document deals with the ID3v2 tag, you can check that an ID3v2 tag exists with


f.hasID3v2Tag();	

In ID3v2, fields are known as frames, for simple text fields you can extract the information with


(v2Tag.getFirst(ID3v24Frames.FRAME_ID_ARTIST));
(v2Tag.getFirst(ID3v24Frames.FRAME_ID_ALBUM));
(v2Tag.getFirst(ID3v24Frames.FRAME_ID_YEAR));

Or you can retrieve the frame itself and then get details of its body with


AbstractID3v2Frame frame = v2Tag.getFirstField(ID3v24Frames.FRAME_ID_ARTIST);
(text.getBody().getId();
if(frame.getBody() instanceof AbstractFrameBodyTextInfo)
{
	AbstractFrameBodyTextInfo textBody = (AbstractFrameBodyTextInfo)frame.getBody(); 
	(text.getBody().getText();
}

The list of all valid identifiers can be found in the class ID3v24Frames