From bd64f70c88542248440d7c2e8787f8bb9cba8b77 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 27 Jan 2024 13:58:11 +0100 Subject: [PATCH] Added audio/video duration --- .../java/eu/faircode/email/AdapterMedia.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMedia.java b/app/src/main/java/eu/faircode/email/AdapterMedia.java index 5c29157b66..d01c133000 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMedia.java +++ b/app/src/main/java/eu/faircode/email/AdapterMedia.java @@ -28,6 +28,7 @@ import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.pdf.PdfRenderer; +import android.media.MediaMetadataRetriever; import android.media.ThumbnailUtils; import android.net.Uri; import android.os.Build; @@ -110,11 +111,6 @@ public class AdapterMedia extends RecyclerView.Adapter tvProperties.setVisibility(View.GONE); if (attachment.available) { - if (attachment.isAudio()) { - showPlayerState(attachment.getUri(context)); - return; - } - Bundle args = new Bundle(); args.putSerializable("file", attachment.getFile(context)); args.putString("type", attachment.getMimeType()); @@ -132,6 +128,22 @@ public class AdapterMedia extends RecyclerView.Adapter String type = args.getString("type"); int max = args.getInt("max"); + args.putLong("size", file.length()); + + if (type != null && + (type.startsWith("audio/") || type.startsWith("video"))) + // https://developer.android.com/reference/android/media/MediaMetadataRetriever + try (MediaMetadataRetriever ret = new MediaMetadataRetriever()) { + ret.setDataSource(file.getAbsolutePath()); + + String value = ret.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); + Integer duration = Helper.parseInt(value); + if (duration != null) + args.putInt("duration", duration); + } catch (Throwable ex) { + Log.w(ex); + } + if ("application/pdf".equals(type)) { // https://developer.android.com/reference/android/graphics/pdf/PdfRenderer try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)) { @@ -172,8 +184,6 @@ public class AdapterMedia extends RecyclerView.Adapter if ("image/webp".equalsIgnoreCase(type) && !webp) return context.getDrawable(R.drawable.twotone_image_not_supported_24); - args.putLong("size", file.length()); - try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; @@ -208,7 +218,9 @@ public class AdapterMedia extends RecyclerView.Adapter @Override protected void onExecuted(Bundle args, Drawable image) { - if (image == null) { + if (attachment.isAudio()) + showPlayerState(attachment.getUri(context)); + else if (image == null) { String type = args.getString("type"); if ("application/pdf".equals(type)) ivImage.setImageResource(R.drawable.twotone_article_24); @@ -249,10 +261,17 @@ public class AdapterMedia extends RecyclerView.Adapter long size = args.getLong("size"); if (size > 0) { if (sb.length() > 0) - sb.append(" \u2013 "); // – + sb.append(' '); sb.append(Helper.humanReadableByteCount(size)); } + int duration = args.getInt("duration"); + if (duration > 0) { + if (sb.length() > 0) + sb.append(' '); + sb.append(Helper.formatDuration(duration)); + } + if (sb.length() > 0) { tvProperties.setText(sb); tvProperties.setVisibility(View.VISIBLE);