I am currently writing a UPnP remote management application that is used to connect a remote MediaServer to a remote MediaRenderer. Since the actual MP3 files are not sent to the Android device, I would like to get the album cover of the currently playing file without downloading the entire MP3 file to my phone.
I read that MediaMetadataRetriever is useful for this kind of thing, but I couldn't get it to work. Every time I try to do this, I continue to get an IllegalArgumentException by calling MediaMetadataRetriever#setDataSource , which indicates that my file descriptor or URI is invalid.
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
The following works because it is the direct path to the file on the device itself:
metaRetriever.setDataSource("/sdcard/Music/Daft_Punk/Homework/01 - Daftendirekt.mp3");
However, any of the following errors with the same error:
metaRetriever.setDataSource(appCtx, Uri.parse("http://192.168.1.144:49153/content/media/object_id/94785/res_id/1/rct/aa")); metaRetriever.setDataSource(appCtx, Uri.parse("http://192.168.1.144:49153/content/media/object_id/94785/res_id/0/ext/file.mp3")); metaRetriever.setDataSource("http://192.168.1.144:49153/content/media/object_id/94785/res_id/0/ext/file.mp3");
The first of these is the ArtURI album, extracted from UPnP metadata (extension without * .mp3, but the file will be downloaded when pasted into a web browser).
The second and third attempts use the "res" value from the UPnP metadata, which points to the actual file on the server.
I hope that I'm just parsing URIs incorrectly, but I have no ideas.
Any suggestions? Also, is there a better way to do this completely when you take from a UPnP server? FWIW, I am using the Cling UPnP library.
== SOLUTION ==
I began to study the answer of william-seemann, and this led me to the following: MediaMetadataRetriever.setDataSource (String path) no longer accepts URLs
Comment # 2 in this post is mentioned using another version of setDataSource() , which still accepts remote URLs.
Here is what I ended up doing and it works great:
private Bitmap downloadBitmap(final String url) { final MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); metaRetriever.setDataSource(url, new HashMap<String, String>()); try { final byte[] art = metaRetriever.getEmbeddedPicture(); return BitmapFactory.decodeByteArray(art, 0, art.length); } catch (Exception e) { Logger.e(LOGTAG, "Couldn't create album art: " + e.getMessage()); return BitmapFactory.decodeResource(getResources(), R.drawable.album_art_missing); } }