From b537dbafe20b1faff45748e8b69080172d089133 Mon Sep 17 00:00:00 2001 From: Dichen Zhang Date: Fri, 18 Oct 2019 14:59:01 -0700 Subject: [PATCH] MediaMetadataRetriever.setDataSource(String path): make this method OK to take URI External comments: https://stackoverflow.com/questions/47240026/mediametadataretriever-setdatasource-return-null MediaMetadataRetriever.setDataSource(String path) can't take URI, whereas same method in MediaPlayer can, which makes it difficult and confusing for the users. Bug: 142966190, 141761504 Test: MediaMetadataRetrieverTest Change-Id: I5ca45cd72b2a77363a49710ed2e578b1de297e40 --- .../java/android/media/MediaMetadataRetriever.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/media/java/android/media/MediaMetadataRetriever.java b/media/java/android/media/MediaMetadataRetriever.java index cc5ddeb49813d..5d2bdd756840d 100644 --- a/media/java/android/media/MediaMetadataRetriever.java +++ b/media/java/android/media/MediaMetadataRetriever.java @@ -34,6 +34,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -62,7 +63,8 @@ public class MediaMetadataRetriever implements AutoCloseable { * method before the rest of the methods in this class. This method may be * time-consuming. * - * @param path The path of the input media file. + * @param path The path, or the URI (doesn't support streaming source currently) + * of the input media file. * @throws IllegalArgumentException If the path is invalid. */ public void setDataSource(String path) throws IllegalArgumentException { @@ -70,6 +72,15 @@ public class MediaMetadataRetriever implements AutoCloseable { throw new IllegalArgumentException("null path"); } + final Uri uri = Uri.parse(path); + final String scheme = uri.getScheme(); + if ("file".equals(scheme)) { + path = uri.getPath(); + } else if (scheme != null) { + setDataSource(path, new HashMap()); + return; + } + try (FileInputStream is = new FileInputStream(path)) { FileDescriptor fd = is.getFD(); setDataSource(fd, 0, 0x7ffffffffffffffL);