Can I get album art from a remote mp3 file in Android?

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); } } 
+6
source share
2 answers

FFmpegMediaMetadataRetriever will extract metadata from a remote file (disclosure: I created it). I have the same interface as MediaMetadataRetriever , but it uses FFmpeg as it happens. Here is an example:

 FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever(); mmr.setDataSource(mUri); String album = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM); String artist = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST); byte [] artwork = mmr.getEmbeddedPicture(); mmr.release(); 
+1
source

Looking at the source code for MediaMetadataRetriever (not from the official repo in Android, but it should still be similar, if not equivalent) showed me this:

 if (uri == null) { throw new IllegalArgumentException(); } 

And this:

 ContentResolver resolver = context.getContentResolver(); try { fd = resolver.openAssetFileDescriptor(uri, "r"); } catch(FileNotFoundException e) { throw new IllegalArgumentException(); } if (fd == null) { throw new IllegalArgumentException(); } FileDescriptor descriptor = fd.getFileDescriptor(); if (!descriptor.valid()) { throw new IllegalArgumentException(); } 

Your exception comes from one of these blocks.

From a look at the MediaMetadataRetriever documentation and the source code, it seems to me that the file should be on the device. You can use Uri , but I think it should be something like "file:///android_asset/mysound.mp3" . I could be wrong; Are you sure MediaMetadataRetriever can be used to resolve files over the network?

0
source

Source: https://habr.com/ru/post/950737/


All Articles