Read ID3 Tags of an MP3 file
Asked Answered
S

3

11

I am trying to read ID3 from a mp3 file thats locally stored in the SD card.

I want to basically fetch

  1. Title
  2. Artist
  3. Album
  4. Track Length
  5. Album Art
Salbu answered 10/6, 2011 at 12:53 Comment(0)
G
30

You can get all of this using MediaMetadataRetriever

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);

String albumName =
     mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
Gymkhana answered 10/6, 2011 at 13:1 Comment(7)
I am not able to import the MediaMetadataRetriever. Eclipse doesn't provide the option. any idea ?Salbu
@Reno: I tried your suggestion, but I get no useful info (basically null), despite the fact that I do see id3 tags with other software...Ossa
FYI, there's a bug in later versions of Android (4.1+) that prevents this from working when using URIs.Wilsonwilt
I want to retrieve Album Art, how can I achieve itPresuppose
Unfortunately, this is broken (get null almost everywhere).Ossa
returns null most of the time, my file has iD3 tag version 2.3 and android kitkatWilmerwilmette
Not working.. getting IllegalArgumentException while calling setDataSource()Tosspot
B
5

Check the MP3 file format. Basically, you have to read the last 128 bytes of the file; if the first 3 bytes are "TAG", carry on and read the fields you need; if not, the file doesn't have the info attached.

Bergren answered 10/6, 2011 at 13:3 Comment(4)
i want to know how to implement it in Android. Any idea ?Salbu
Create a InputStream from the file and a 128 byte buffer; read the last 128 bytes into the buffer; check the first 3 bytes of the buffer, should be TAG; read the relevant bytes using Arrays.copyOfRange; convert those bytes to int (for genre) or String (for everything else).Bergren
It's not really that simple. There are more versions of tags, this easy you read only old version.Private
Yeah, the "TAG" is ID3v1 and in IDv2 you have two versions as well. Tags that are 3 characters and others that are 4 characters...Hako
E
4

if the last 128 byte begins with the token "TAG" the file has an ID3v1 (or ID3v1.1) tag. ID3v2.3 tags are located in the beginning of the file (suitable for streaming) indicated by the token "id3". I believe ID3v2.4 is indicated by "3DE", but i'm not sure...

Empirin answered 22/4, 2013 at 8:32 Comment(1)
This was exactly the information I needed, thank you! I was trying to pattern match Bandcamp MP3s with Elixir and always got an error, because Bandcamp MP3s are ID3v2.3 tagged. Would upvote a hundred times, if I could.Phono

© 2022 - 2024 — McMap. All rights reserved.