summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorByteHamster <info@bytehamster.com>2023-07-18 17:01:22 +0200
committerByteHamster <ByteHamster@users.noreply.github.com>2023-07-18 17:57:40 +0200
commit9f8edd0e9d147ec13532143ae3174d3f71e8219a (patch)
tree4e86e482e3c1f5e1856de4611c02154f8c815127
parent9be6562b4e4bd4a1487a02a92c68db3625e19d99 (diff)
downloadAntennaPod-9f8edd0e9d147ec13532143ae3174d3f71e8219a.zip
Fix MediaMetadataRetriever on API<29
On SDK<29, this class does not have a close method yet, so the app crashes when using try-with-resources.
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java5
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java3
-rw-r--r--model/src/main/java/de/danoeh/antennapod/model/MediaMetadataRetrieverCompat.java18
-rw-r--r--model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java7
-rw-r--r--ui/glide/src/main/java/de/danoeh/antennapod/ui/glide/AudioCoverFetcher.java4
5 files changed, 28 insertions, 9 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java
index 03881ee4f..eae7a8426 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java
@@ -28,6 +28,7 @@ import androidx.annotation.VisibleForTesting;
import androidx.documentfile.provider.DocumentFile;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.util.FastDocumentFile;
+import de.danoeh.antennapod.model.MediaMetadataRetrieverCompat;
import de.danoeh.antennapod.model.download.DownloadResult;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
@@ -199,8 +200,8 @@ public class LocalFeedUpdater {
return item;
}
- private static void loadMetadata(FeedItem item, FastDocumentFile file, Context context) throws IOException {
- try (MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever()) {
+ private static void loadMetadata(FeedItem item, FastDocumentFile file, Context context) {
+ try (MediaMetadataRetrieverCompat mediaMetadataRetriever = new MediaMetadataRetrieverCompat()) {
mediaMetadataRetriever.setDataSource(context, file.getUri());
String dateStr = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java
index a46b4c6d0..019f311d2 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/MediaDownloadedHandler.java
@@ -6,6 +6,7 @@ import android.util.Log;
import androidx.annotation.NonNull;
+import de.danoeh.antennapod.model.MediaMetadataRetrieverCompat;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
@@ -63,7 +64,7 @@ public class MediaDownloadedHandler implements Runnable {
}
// Get duration
String durationStr = null;
- try (MediaMetadataRetriever mmr = new MediaMetadataRetriever()) {
+ try (MediaMetadataRetrieverCompat mmr = new MediaMetadataRetrieverCompat()) {
mmr.setDataSource(media.getFile_url());
durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
media.setDuration(Integer.parseInt(durationStr));
diff --git a/model/src/main/java/de/danoeh/antennapod/model/MediaMetadataRetrieverCompat.java b/model/src/main/java/de/danoeh/antennapod/model/MediaMetadataRetrieverCompat.java
new file mode 100644
index 000000000..f3173946d
--- /dev/null
+++ b/model/src/main/java/de/danoeh/antennapod/model/MediaMetadataRetrieverCompat.java
@@ -0,0 +1,18 @@
+package de.danoeh.antennapod.model;
+
+import android.media.MediaMetadataRetriever;
+
+import java.io.IOException;
+
+/**
+ * On SDK<29, this class does not have a close method yet, so the app crashes when using try-with-resources.
+ */
+public class MediaMetadataRetrieverCompat extends MediaMetadataRetriever {
+ public void close() {
+ try {
+ release();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java
index 9e8583075..991b5f3db 100644
--- a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java
+++ b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java
@@ -2,13 +2,13 @@ package de.danoeh.antennapod.model.feed;
import android.content.Context;
import android.content.SharedPreferences.Editor;
-import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.Nullable;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaDescriptionCompat;
+import de.danoeh.antennapod.model.MediaMetadataRetrieverCompat;
import de.danoeh.antennapod.model.playback.MediaType;
import de.danoeh.antennapod.model.playback.Playable;
import de.danoeh.antennapod.model.playback.RemoteMedia;
@@ -458,11 +458,10 @@ public class FeedMedia extends FeedFile implements Playable {
hasEmbeddedPicture = Boolean.FALSE;
return;
}
- MediaMetadataRetriever mmr = new MediaMetadataRetriever();
- try {
+ try (MediaMetadataRetrieverCompat mmr = new MediaMetadataRetrieverCompat()) {
mmr.setDataSource(getLocalMediaUrl());
byte[] image = mmr.getEmbeddedPicture();
- if(image != null) {
+ if (image != null) {
hasEmbeddedPicture = Boolean.TRUE;
} else {
hasEmbeddedPicture = Boolean.FALSE;
diff --git a/ui/glide/src/main/java/de/danoeh/antennapod/ui/glide/AudioCoverFetcher.java b/ui/glide/src/main/java/de/danoeh/antennapod/ui/glide/AudioCoverFetcher.java
index d8e265ab5..309afa39d 100644
--- a/ui/glide/src/main/java/de/danoeh/antennapod/ui/glide/AudioCoverFetcher.java
+++ b/ui/glide/src/main/java/de/danoeh/antennapod/ui/glide/AudioCoverFetcher.java
@@ -2,13 +2,13 @@ package de.danoeh.antennapod.ui.glide;
import android.content.ContentResolver;
import android.content.Context;
-import android.media.MediaMetadataRetriever;
import android.net.Uri;
import androidx.annotation.NonNull;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.data.DataFetcher;
+import de.danoeh.antennapod.model.MediaMetadataRetrieverCompat;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@@ -25,7 +25,7 @@ class AudioCoverFetcher implements DataFetcher<InputStream> {
@Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
- try (MediaMetadataRetriever retriever = new MediaMetadataRetriever()) {
+ try (MediaMetadataRetrieverCompat retriever = new MediaMetadataRetrieverCompat()) {
if (path.startsWith(ContentResolver.SCHEME_CONTENT)) {
retriever.setDataSource(context, Uri.parse(path));
} else {