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.
This commit is contained in:
ByteHamster 2023-07-18 17:01:22 +02:00 committed by ByteHamster
parent 9be6562b4e
commit 9f8edd0e9d
5 changed files with 28 additions and 9 deletions

View File

@ -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);

View File

@ -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));

View File

@ -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();
}
}
}

View File

@ -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;

View File

@ -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 {