Extract album cover from mp3 file in android - android

Hello Everyone ,
In my media player i need to display the album cover(i dont know how it pronounced actually..I hope right) of the song. I knew for that i have to extract the image from the song itself but how? m wondering. So any help, if possible with some sorts of code. Thanks.

for api 10 and above
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(songsList.get(songIndex).get("songPath"));
byte [] data = mmr.getEmbeddedPicture();
//coverart is an Imageview object
// convert the byte array to a bitmap
if(data != null)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
coverart.setImageBitmap(bitmap); //associated cover art in bitmap
}
else
{
coverart.setImageResource(R.drawable.fallback_cover); //any default cover resourse folder
}
coverart.setAdjustViewBounds(true);
coverart.setLayoutParams(new LinearLayout.LayoutParams(500, 500));

Try FFmpegMediaMetadataRetriever:
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(uri);
byte [] data = retriever.getEmbeddedPicture();
// convert the byte array to a bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// do something with the image ...
// mImageView.setImageBitmap(bitmap);
retriever.release();

You can try with Picasso by using album_id. it is open source & less memory cache.
Dependency:
implementation 'com.squareup.picasso:picasso:2.71828'
Code:
String albumId = songObject.getAlbum_id();
final Uri albumUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(albumUri, Long.parseLong(albumId));
Picasso.get().load(uri)
.fit()
.centerCrop()
.error(R.drawable.img_album)
.into(holder.imgAlbumSongObject);
This is very late. but, may help someone.

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
String albumName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
Note this will work only at api level 10 or above

Related

Set thumbnail to an Intent for VideoView

I have to play a video in one of my screens of Android and I am using Video View intent for the same. The video gets played but there is no thumbnail appearing on the launch of the screen.
My code is like this
#OnClick(R.id.icon_play)
protected void playVideo(){
String videoUrl="https://someUrl/Video/v07.mp4";
if(!videoUrl.isEmpty()) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl));
intent.setDataAndType(Uri.parse(videoUrl), "video/mp4");
startActivity(intent);
}
}
By thumbnail, I mean that when the screen is launched each video should have an image of its own. (something like YouTube)
I tried seekTo() for attaching the thumbnail, but it didn't work.
Please Help. Thanks.
I solve the problem using MediaMetaDataRetriever.
The code goes like this-
public static Bitmap retriveVideoFrameFromVideo(String videoPath)
throws Throwable
{
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try
{
mediaMetadataRetriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
else
mediaMetadataRetriever.setDataSource(videoPath);
// mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime(-1,MediaMetadataRetriever.OPTION_CLOSEST);
}
catch (Exception e)
{
e.printStackTrace();
throw new Throwable(
"Exception in retriveVideoFrameFromVideo(String videoPath)"
+ e.getMessage());
}
finally
{
if (mediaMetadataRetriever != null)
{
mediaMetadataRetriever.release();
}
}
return bitmap;
}
Note that : Because my video link was in the form of server URL, that's why createThumnailUtils was returning a null when video Url was passed through it.
The below code works fine when the video is coming from local storage.
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail("URL", MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bitmapD = new BitmapDrawable(thumbnail);
VideoView.setBackground(Drawable bitmapD);
Hope this helps someone!!
Just looked at another example. Not 100% sure if it's going to work but worth a try.
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail("URL", MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bitmapD = new BitmapDrawable(thumbnail);
VideoView.setBackground(Drawable bitmapD);
Please note, I have written this over phone so there might be spelling errors.
Let me know if this works or if you find another alternative
this may be used i am using this method for thumbnail image of video on my list view of video player..
Cursor cursor = mContext.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,
MediaStore.Video.Media.DISPLAY_NAME + "=?",
new String[]{localItem._display_name}, null);
cursor.moveToFirst();
long ids = cursor.getLong(cursor
.getColumnIndex(MediaStore.Video.Media._ID));
ContentResolver crThumb = mContext.getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(
crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND,
options);
itemHolder.thumbImage.setImageBitmap(curThumb);
curThumb = null;
cursor.close();
try this way it may be helpful
above ans is now deprecated.
new updated answer with fast one solution
we just have to give the video data path
Bitmap bmThumbnail;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(arraylist.get(i)._data, MediaStore.Video.Thumbnails.MICRO_KIND);
itemHolder.thumbImage.setImageBitmap(bmThumbnail);
where arraylist.get(i)._data => path of video.
and best way is to use Glide or any other image lodging async library for smooth scrolling of listview.

How to get frame of stream video link with Glide

in my android TV application, at start i want to show a frame of my each stream links. my current solution is using LoaderManager and the problem of this technique is to it's too slow(application crashed).
FFmpegMediaMetadataRetriever to load and set video stream link.
this.retriever = new FFmpegMediaMetadataRetriever();
Bitmap bitmap = null;
retriever.setDataSource(this.mSelectedStream.getStreamUrl());
bitmap = retriever.getFrameAtTime();
drawable = new BitmapDrawable(bitmap);
retriever.release();
i found this thread that explain that glide can used to load image from video links.
BitmapPool bitmapPool = Glide.get(getApplicationContext()).getBitmapPool();
int microSecond = 6000000;// 6th second as an example
VideoBitmapDecoder videoBitmapDecoder = new VideoBitmapDecoder(microSecond);
FileDescriptorBitmapDecoder fileDescriptorBitmapDecoder = new FileDescriptorBitmapDecoder(videoBitmapDecoder, bitmapPool, DecodeFormat.PREFER_ARGB_8888);
Glide.with(getApplicationContext())
.load(yourUri)
.asBitmap()
.override(50,50)// Example
.videoDecoder(fileDescriptorBitmapDecoder)
.into(yourImageView);
but when i used above code i get "cannot access from outside of package" error for VideoBitmapDecoder.
exmaple link = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"
any idea?
thanks
I've used this snippet to get thumb nail out of video frames. Try it
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(media_url,
MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable BD = new BitmapDrawable(thumbnail);
videoView.setBackgroundDrawable(BD);

Album art not displayed in music player in android

I'm building a music player in Android. I'm displaying the album art for currently playing song. This is my code to retrieve the embedded image:-
public Bitmap getAlbumArt(String filePath) {
Bitmap bm = null;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
byte [] data = mmr.getEmbeddedPicture();
// convert the byte array to a bitmap
if(data != null) {
bm = BitmapFactory.decodeByteArray(data, 0, data.length);
}
return bm;
}
This code works fine and is able to retrieve album art but only for some songs. However, I can see the album art for all the songs in thumbnail view in Windows.
Could somebody explain what might be the reason behind this? Also, if there is an alternate way to achieve the same?
Thank You!

Get cover picture by song

Is it possible to get a cover picture by song and not by album.
Because I have one self combined album with songs and they all have different cover pictures.
But when I want to query them I always get the same picture returned.
String[] ARG_STRING = {MediaStore.Audio.Media.ALBUM_ID};
...
String albumCover = _cursor.getString(_cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
...
MusicUtils.getArtwork(this, -1, Integer.parseInt(albumID));
So i would like to know how it's possible to get an cover image of an song.
I know MusicUtils supports getArtwork by SongId, but what ID should I use because MediaStore.Audio.Media._ID is not working.
I'm not familiar with MusicUtils, however, you should be able to get the cover art from the file itself by using MediaMetadataRetriever. Here is a brief code snippet showing how to use it. The uri referenced is the content uri for the file you want to retrieve the art for.
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt;
Bitmap art;
BitmapFactory.Options bfo=new BitmapFactory.Options();
mmr.setDataSource(getApplicationContext(), uri);
rawArt = mmr.getEmbeddedPicture();
// if rawArt is null then no cover art is embedded in the file or is not
// recognized as such.
if (null != rawArt)
art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
// Code that uses the cover art retrieved below.

How to extract metadata from mp3?

I've got file name of mp3 file. How can I extract metadata like artist, album, album image,... from this mp3 file?
try this for API level 10 or greater
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
String albumName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
and so on...
for more help
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(songsList.get(songIndex).get("songPath"));
byte[] artBytes = mmr.getEmbeddedPicture();
if(artBytes!=null)
{
// InputStream is = new ByteArrayInputStream(mmr.getEmbeddedPicture());
Bitmap bm = BitmapFactory.decodeByteArray(artBytes, 0, artBytes.length);
bSongImage.setImageBitmap(bm);
}
else
{
bSongImage.setImageDrawable(getResources().getDrawable(R.drawable.cmp));
}
else for not having embedded image in audio file
MetaDataRetriever m_metaRetriever = new MetaDataRetriever();
m_metaRetriever.setDataSource(MainActivity.this,uriSound);
The input parameter of the setDataSource method should include the context also.Else it throws illigalArgument Exception.

Categories

Resources