Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get embedded mp3 file embedded art failed

I'm trying to get the album art of a MP3 file. I thought the best and cleanest way to do this is use the MediaMetadataRetriever class. But for some reason calling the getEmbeddedPicture method doesn't work. The image isn't showing, LogCat shows an error:

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

This is the code that doesn't 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'm running it on a device with Android 4.2, so there shouldn't be any issue with the MediaMetadataRetriever(requires api lvl 10). The files I tested show an image in Windows explorer, so there seems to be art embedded. Anyone have any thoughts on this?

like image 861
Raymond P Avatar asked Apr 29 '13 17:04

Raymond P


1 Answers

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

like image 140
Mani Avatar answered Sep 20 '22 23:09

Mani