how can get album art of song from url

2019-09-08 16:49发布

问题:

want get album art of song from url and this is my try so far :

SongPath = "www.asd.com/music.mp3";
        android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    try{
                mmr.setDataSource(SongPath);
        }catch(Exception e){}
                byte [] data = mmr.getEmbeddedPicture();

                if(data != null)
                {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    imageView.setImageBitmap(bitmap);
                }
                else
                {
                    imageView.setImageResource(R.drawable.jak);
                }

but when run this code get this : call to get embedded picture failed so i research about this and some people fix that with change this part
mmr.setDataSource(SongPath);

to this

mmr.setDataSource(SongPath,new HashMap<String, String>()); i do that but when run app image view show nothing and get this : SkImageDecoder::Factory returned null

Note : the only way i could do that use FFmpegMediaMetadataRetriever library (its like MediaMetadataRetriever) and worked but problem is library seems slow .. its mean 4,5sec time need to fetch pic and when add this library the apk file from 1.8mb become to 24mb! and this is so huge!

so any one in the world know how can do that with good way ? if any one can please help

回答1:

You can use Glide in a similar way you would use it for a File: bumptech/glide#699 if you can figure out how to do it remotely. The problem is that you still need to download the whole .mp3 file, which takes that 4-5 seconds I guess. I'm not exactly sure about the .mp3 format, but I think the album art may be at the end of the file. For this reason this approach is not suggested (similar to how it's a bad idea to load video thumbs from http). If you want to go this way anyway, then slap a .diskCacheStrategy(SOURCE) on the load to save the file first, and then write a custom decoder to use

The best approach is to have the album art served from a separate file if you host the mp3; or use a public service to retrieve it. See any album art downloader program for possible services.

One thing's for sure: without a protocol, nothing will work, prefix your www. with http:// or https:// as necessary.