Embedding an embedded mp3 file

I am trying to get an album cover in MP3 format. I thought the best and cleanest way to do this is to use the MediaMetadataRetriever class. But for some reason, calling the getEmbeddedPicture method does not work. The image is not displayed, LogCat shows an error:

04-29 18:36:19.520: E/MediaMetadataRetrieverJNI(25661): getEmbeddedPicture: Call to getEmbeddedPicture failed.

This is the code that does not seem to work:

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        MediaMetadataRetriever mmdr = new MediaMetadataRetriever();
        mmdr.setDataSource(path); //path of the MP3 file on SD Card
        bites = mmdr.getEmbeddedPicture();
        if(bites != null)
        artBM = BitmapFactory.decodeByteArray(bites, 0, bites.length);
        return null;
    }

I run it on a device with Android 4.2, so there should not be any problems with MediaMetadataRetriever (api lvl 10 required). The files I tested show an image in Windows Explorer, so it seems to be an embedded art. Anyone have any thoughts on this?

+5
source share
2 answers

MP3 , , ,

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

, . , , , [] ,

[] null, , null,

Im,

    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));
    }

,

+7

, , mp3 . .

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;
    }
0

All Articles