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!
Related
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.
Hi am working on a video app in android i want to generate video thumbnail and send to the server or simple how can i get video thumbnail and store in server so that when i retrieve the video i can also get the video thumbnail to use in a recycle view thanks
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bitmapDrawable = new BitmapDrawable(thumb);
vidPreview.setBackgroundDrawable(bitmapDrawable);
I assume you are sending the video to the server also? If so then it may be better to generate the thumbnail on the server as you usually have more processing power there and less worry about consuming battery. It also saves you having to send the generated thumbnail to the server.
If you do want to create the thumbnail on the Android device then the following code will work (before this chunk the app has loaded all the videos in Media Store using the loader pattern and they are accessible via the 'cursor' variable below) - see the 'getThumbnail' method call:
while (videoCursor.moveToNext()) {
//Create the Thumbnail for this video
Log.d("ItemListFragment", "onLoadFinished: Creating Thumbnail");
String videoTitle = videoCursor.getString(titleColumn_index);
String videoPath = videoCursor.getString(pathColumn_index);
long videoID = videoCursor.getLong(idColumn_index);
Bitmap thisVideoThumbnail = MediaStore.Video.Thumbnails.getThumbnail(this.getActivity().getContentResolver(), videoID, MediaStore.Images.Thumbnails.MINI_KIND, null);
if (thisVideoThumbnail == null) {
Log.d("VideoContent refresh ","VideoThumbnail is null!!!");
}
VideoItem newVideoItem = new VideoItem(videoID, videoTitle, videoPath, thisVideoThumbnail);
//Add the new video item to the list
videosArray.addItem(newVideoItem);
}
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.
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
I am developing a sort of media player for android. The question is how can i get the cover art of audio file on android.
For example the default android media player shows album covers when listing albums, how can i get this artworks.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.
You can use
MediaMetadataRetriever
class and get track info i.e. Title,Artist,Album,Image
Bitmap GetImage(String filepath) //filepath is path of music file
{
Bitmap image;
MediaMetadataRetriever mData=new MediaMetadataRetriever();
mData.setDataSource(filePath);
try{
byte art[]=mData.getEmbeddedPicture();
image=BitmapFactory.decodeByteArray(art, 0, art.length);
}
catch(Exception e)
{
image=null;
}
return image;
}
I don't know why everybody is making it so complicated you can use Glide to achieve this in simplest and efficient way with just 1 line of code
Declare this path in App Constants -
final public static Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Get Image Uri using Album ID
Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
listOfAlbums.get(position).getAlbumID());
Now simply display album art using uri :-
Glide.with(context).load(uri).placeholder(R.drawable.art_default).error(R.drawable.art_default)
.crossFade().centerCrop().into(holder.albumImage);
Glide will handle caching, scaling and lazy loading of images for you.
Hope it help somebody.
Here i can attach one function that is return album art from media store .
Here in function we just have to pass the album_id which we get from Media store .
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
Based on your comments to others, it seems like your question is less about Android and more about how to get album art in general. Perhaps this article on retrieving album art from Amazon will be helpful. Once you have a local copy and store it as Nick has suggested, I believe you should be able to retrieve it the way Fudgey suggested.
Android only recognizes files named "AlbumArt.jpg" as Album Covers. Just put the pictures with that name in the album folder and you'll be fine..
I don't know if you read that google is making Stackoverflow the official Android app development Q&A medium but for beginner questions... Now, I know nothing about developing in andriod, but a quick search of the Android Developers site, I found this:
MediaStore.Audio.AlbumColumns
Hopefully it'll help.