From 0400bda7cd65c811e1e7932779c846e6668585da Mon Sep 17 00:00:00 2001 From: Martin Disch Date: Wed, 20 Jun 2018 14:16:41 +0200 Subject: [PATCH 1/2] Fix loading of large embedded album art Android's MediaMetadataRetriever.getEmbeddedPicture() fails on large images, because ID3 metadata that is considered too big is skipped. --- app/build.gradle | 1 + .../audiocover/AudioFileCoverFetcher.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 665d52d4..1ee84c29 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -137,5 +137,6 @@ dependencies { transitive = true } implementation 'com.google.code.gson:gson:2.8.2' + implementation 'com.mpatric:mp3agic:0.9.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' } diff --git a/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java b/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java index a675a869..930548a2 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java +++ b/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java @@ -4,6 +4,10 @@ import android.media.MediaMetadataRetriever; import com.bumptech.glide.Priority; import com.bumptech.glide.load.data.DataFetcher; +import com.mpatric.mp3agic.ID3v2; +import com.mpatric.mp3agic.InvalidDataException; +import com.mpatric.mp3agic.Mp3File; +import com.mpatric.mp3agic.UnsupportedTagException; import java.io.ByteArrayInputStream; import java.io.File; @@ -48,6 +52,23 @@ public class AudioFileCoverFetcher implements DataFetcher { private static final String[] FALLBACKS = {"cover.jpg", "album.jpg", "folder.jpg", "cover.png", "album.png", "folder.png"}; private InputStream fallback(String path) throws FileNotFoundException { + // Method 1: use embedded high resolution album art if there is any + try { + Mp3File mp3File = new Mp3File(path); + if (mp3File.hasId3v2Tag()) { + ID3v2 id3v2Tag = mp3File.getId3v2Tag(); + byte[] imageData = id3v2Tag.getAlbumImage(); + if (imageData != null) { + return new ByteArrayInputStream(imageData); + } + } + // If there are any exceptions, we ignore them and continue to the other fallback method + } catch (IOException ignored) { + } catch (InvalidDataException ignored) { + } catch (UnsupportedTagException ignored) { + } + + // Method 2: look for album art in external files File parent = new File(path).getParentFile(); for (String fallback : FALLBACKS) { File cover = new File(parent, fallback); From 56a6ff56f038af853fe27308f6462a30d60bf2c1 Mon Sep 17 00:00:00 2001 From: Martin Disch Date: Wed, 20 Jun 2018 15:15:35 +0200 Subject: [PATCH 2/2] Use JAudiotagger instead of mp3agic --- app/build.gradle | 1 - .../audiocover/AudioFileCoverFetcher.java | 25 +++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1ee84c29..665d52d4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -137,6 +137,5 @@ dependencies { transitive = true } implementation 'com.google.code.gson:gson:2.8.2' - implementation 'com.mpatric:mp3agic:0.9.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' } diff --git a/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java b/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java index 930548a2..efacb32f 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java +++ b/app/src/main/java/com/kabouzeid/gramophone/glide/audiocover/AudioFileCoverFetcher.java @@ -4,10 +4,12 @@ import android.media.MediaMetadataRetriever; import com.bumptech.glide.Priority; import com.bumptech.glide.load.data.DataFetcher; -import com.mpatric.mp3agic.ID3v2; -import com.mpatric.mp3agic.InvalidDataException; -import com.mpatric.mp3agic.Mp3File; -import com.mpatric.mp3agic.UnsupportedTagException; + +import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; +import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; +import org.jaudiotagger.audio.mp3.MP3File; +import org.jaudiotagger.tag.TagException; +import org.jaudiotagger.tag.images.Artwork; import java.io.ByteArrayInputStream; import java.io.File; @@ -54,18 +56,19 @@ public class AudioFileCoverFetcher implements DataFetcher { private InputStream fallback(String path) throws FileNotFoundException { // Method 1: use embedded high resolution album art if there is any try { - Mp3File mp3File = new Mp3File(path); - if (mp3File.hasId3v2Tag()) { - ID3v2 id3v2Tag = mp3File.getId3v2Tag(); - byte[] imageData = id3v2Tag.getAlbumImage(); - if (imageData != null) { + MP3File mp3File = new MP3File(path); + if (mp3File.hasID3v2Tag()) { + Artwork art = mp3File.getTag().getFirstArtwork(); + if (art != null) { + byte[] imageData = art.getBinaryData(); return new ByteArrayInputStream(imageData); } } // If there are any exceptions, we ignore them and continue to the other fallback method + } catch (ReadOnlyFileException ignored) { + } catch (InvalidAudioFrameException ignored) { + } catch (TagException ignored) { } catch (IOException ignored) { - } catch (InvalidDataException ignored) { - } catch (UnsupportedTagException ignored) { } // Method 2: look for album art in external files