问题
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?
回答1:
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
回答2:
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;
}
来源:https://stackoverflow.com/questions/16284241/get-embedded-mp3-file-embedded-art-failed