Get embedded mp3 file embedded art failed

。_饼干妹妹 提交于 2019-12-05 10:43:21

Not all MP3 files have Album art embedded, for some albums the Album art is placed inside the album folder, so you can see album art for all the files inside that folder,

But

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(mp3_file_path); 

This will get the Album art if the Album art is embedded in that file, So make a default image as album art for files which are not embedded with Album art, and check if the returned byte[] is null or not,

If the byte[] is not null then Album art is retrieved, if it is null then set the default album art image

In my Project Im using this

    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(songsList.get(index).get("songPath"));
    byte[] artBytes =  mmr.getEmbeddedPicture();
    if(artBytes != null)
    {
        InputStream is = new ByteArrayInputStream(mmr.getEmbeddedPicture());
        Bitmap bm = BitmapFactory.decodeStream(is);
        imgArt.setImageBitmap(bm);
    }
    else
    {
        imgArt.setImageDrawable(getResources().getDrawable(R.drawable.adele));
    }

I hope this will help you

I get the same problem,it seems that not all mp3 file has a Album art. What we should do is to set a default picture to the Image.

public Bitmap getAlbumBitmap(Context context, String url, int defaultRes) {
        Bitmap bitmap = null;
        //能够获取多媒体文件元数据的类
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(url); //设置数据源
            byte[] embedPic = retriever.getEmbeddedPicture(); //得到字节型数据
            bitmap = BitmapFactory.decodeByteArray(embedPic, 0, embedPic.length); //转换为图片
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return bitmap == null ? BitmapFactory.decodeResource(context.getResources(), defaultRes) : bitmap;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!