Skip to content

Instantly share code, notes, and snippets.

@laithnurie
Created December 8, 2015 22:38
Show Gist options
  • Save laithnurie/544802b49e818575e00a to your computer and use it in GitHub Desktop.
Save laithnurie/544802b49e818575e00a to your computer and use it in GitHub Desktop.
extract meta data from mp3 files
String artist = null;
String album = null;
String track = null;
byte[] imageData = null;
long duration = 0;
try {
Mp3File mp3file = new Mp3File(songLocation);
if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
imageData = id3v2Tag.getAlbumImage();
artist = (id3v2Tag.getArtist() != null && !id3v2Tag.getArtist().isEmpty() ? id3v2Tag.getArtist() : null);
album = (id3v2Tag.getAlbum() != null && !id3v2Tag.getAlbum().isEmpty() ? id3v2Tag.getAlbum() : null);
track = (id3v2Tag.getTitle() != null && !id3v2Tag.getTitle().isEmpty() ? id3v2Tag.getTitle() : null);
}
if (mp3file.hasId3v1Tag()) {
ID3v1 id3v1Tag = mp3file.getId3v1Tag();
if (artist == null) {
artist = (id3v1Tag.getArtist() != null && !id3v1Tag.getArtist().isEmpty() ? id3v1Tag.getArtist() : null);
}
if (album == null) {
album = (id3v1Tag.getAlbum() != null && !id3v1Tag.getAlbum().isEmpty() ? id3v1Tag.getAlbum() : null);
}
if (track == null) {
track = (id3v1Tag.getTitle() != null && !id3v1Tag.getTitle().isEmpty() ? id3v1Tag.getTitle() : null);
}
}
artist = artist != null ? artist : "Unknown Artist";
album = album != null ? album : "Unknown Album";
track = track != null ? track : "Unknown Track";
duration = mp3file.getLengthInSeconds();
} catch (IOException | UnsupportedTagException | InvalidDataException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment