How to use the Java MP3 ID3 Tag Library to retrieve album artwork
Asked Answered
E

3

17

I'm making an mp3 player. I'm using the Java mp3 id3 tag library. I understand that album artwork is encoded as a ID3v2 tag.

I can access the ID3v2 tag of a mp3 file however I cannot get the artwork! None of the methods in the AbstractID3v2 class, in the API seem to retrieve a picture.

How does one use this library?

Esmaria answered 6/2, 2013 at 12:2 Comment(0)
E
28

I ended up using another library, I used mp3agic

It's a great library which is easy to use. Here's sample code I used to get the album artwork

Mp3File song = new Mp3File(filename);
if (song.hasId3v2Tag()){
     ID3v2 id3v2tag = song.getId3v2Tag();
     byte[] imageData = id3v2tag.getAlbumImage();
     //converting the bytes to an image
     BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData));
}
Esmaria answered 7/2, 2013 at 10:55 Comment(2)
I am unable to import that library. I tried both id3-1.6.0d9.jar and jid3lib-0.5.4.jar, but neither has Mp3File.Sain
Does it work for other audio file formats too? Or need to find those as well? Do other audio file formats even have something similar to this?Tedder
H
3

There's also Jaudiotagger which can read/write Mp3, Mp4 (Mp4 audio, M4a and M4p audio) Ogg Vorbis, Flac and Wma + some others (album art too).

MP3File f = (Mp3File)AudioFileIO.read(testFile);
List<Artwork> artworkList;
if (f.hasID3v1Tag()) {
    ID3v1Tag v1tag = f.getID3v1Tag();
    artworkList = (List<Artwork>) v1tag.getArtworkList();
    /* ... */
}
Hyson answered 31/1, 2014 at 12:16 Comment(1)
I think that library is broken. I get: Could not find class 'sun.nio.ch.DirectBuffer', referenced from method org.jaudiotagger.audio.mp3.MP3File.readV2TagSain
P
0

You can do it using mp3agic and this is another way!

Get Jar here: https://jar-download.com/artifacts/com.mpatric/mp3agic/0.9.0/source-code

Mp3File mp3file = new Mp3File("SomeMp3File.mp3");
if (mp3file.hasId3v2Tag()) {
   ID3v2 id3v2Tag = mp3file.getId3v2Tag();
   byte[] imageData = id3v2Tag.getAlbumImage();
}

if (imageData != null) {
   String mimeType = id3v2Tag.getAlbumImageMimeType();
   RandomAccessFile file = new RandomAccessFile("album-artwork", "rw");
   file.write(data);
   file.close();
}
Pansophy answered 18/7, 2020 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.