How to extract metadata from mp3? - android

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.

Related

Get rating metadata form a list of song (MP3) in android using android.media.Rating

I need to retrieve the rating from a list of all the songs in a phone.
Currently I have them on a File List. Some metadata can be listed with MediaMetadataRetriever but the ratings saved in the song by BlackPlayer EX can't, so I need to extract them with another method.
I've found "android.media.Rating" but with no luck.
My code so far:
String musicPath = Environment.getExternalStorageDirectory().toString() + "/Music";
textView.setText(musicPath);
File directory = new File(musicPath);
textView.setText("On it");
MP3FileFilter fileFilter = new MP3FileFilter();
List songsFiles = listFiles(directory, fileFilter, true);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
for(int i = 0; i<songsFiles.size(); i++){
String song = songsFiles.get(i).toString();
mmr.setDataSource(song);
((Rating) songsFiles.get(i)).getStarRating();
}
textView.setText("Done");
The error displaying is pretty obvious but I can't find a way to solve it:
java.io.File cannot be cast to android.media.Rating
Thanks in advance.
Got it working with JAudioTagger jar and this code:
MP3File musicFile = (MP3File) AudioFileIO.read((File) songsFiles.get(i));
if (musicFile != null && musicFile.hasID3v2Tag()) {
ID3v23Frame frame = (ID3v23Frame)
musicFile.getID3v2Tag().getFrame(ID3v24Frames.FRAME_ID_POPULARIMETER);
FrameBodyPOPM body = (FrameBodyPOPM) frame.getBody();
Long irating = body.getRating();
}

How can I get the duration, resolution of a video file programmatically in Android?

I hope to get the duration, resolution of a video file of a video (.mp4), how can I do that programmatically in Android ? My minSdkVersion is 21 .
BTW, I can get the file size of a video using the following code.
String recVideoPath = Environment.getExternalStorageDirectory() + videoRecordedFileName;
File file = new File(recVideoPath);
long fileVideo = file.length();
Thanks!
To get duration and resolution MediaMetadataRetriever could be used like:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uriOfFile);
long duration = Long.parseLong(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();
uriOfFile is a string with the url to you mp4.

MediaMetadataRetriever returns null on S3

I have the problem that MediaMetadataRetriever always returns null for the title, but only on stock S3. It's working with CyanogenMod on S3 but not with Samsungs stock rom. Also, on my OnePlus is everything working fine.
The Code is very simple:
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
String titlename = fields[count].getName();
final Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + titlename);
mmr.setDataSource(MainActivity.this, uri);
final String name = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
[...]
tv.setText(name + " ");
The TextView will show null on Samsung stock ROM, but not on other ROMs.
This is a bit strange, does someone here have an idea? If not, I'll try a third-party library for ID3 tags.
I noticed the same issue on ICS (On Galaxy SII and Galaxy Tab II both running ICS 4.0.3). This seems to impact only mp3.
I guess one of the solutions would be to use an external library but I also prefer to use what android offers rather then external libraries.
I have tried two solutions:
MediaMetadataRetriever mmdr = new MediaMetadataRetriever();
mmdr.setDataSource(path);
String title = mmdr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
And
File file = new File(path);
FileInputStream inputStream;
inputStream = new FileInputStream(file);
mmdr = new MediaMetadataRetriever();
mmdr.setDataSource(inputStream.getFD());
inputStream.close();
String title = mmdr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
But the problem persists:
MediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) always retuns null.
A solution that I thought of apart from using an external library would be to query the MediaStore on the file's path:
Cursor c = mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.MediaColumns.TITLE},
MediaStore.MediaColumns.DATA + "=?",
new String[] { path }, null);
String title;
if(c!=null && c.moveToFirst())
title = c.getString(c.getColumnIndex(MediaStore.MediaColumns.TITLE))
}
If the MediaScanner scanned it (it should have), the info should be there. This should also work for API levels before 10.
Basically, what to do is the following: If SDK version is < 10 or the file's extension is mp3 and SDK version is 15, I query the MediaStore, otherwise I use MediaMetaDataRetriever.
Also you can try this
if (Build.VERSION.SDK_INT >= 14) {
mmr = new MediaMetadataRetriever();
mmr.setDataSource(audio.url, new HashMap<String, String>());
} else {
mmr = new MediaMetadataRetriever();
mmr.setDataSource(audio.url);
}
This is what I have tried so far to solve the issue. But I can't guarantee if it is going to work for you.
Hope I helped.
EDIT: You need to pass path or file descriptor as an argument to setDataSource. Try this code:
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.music);
if (afd != null) {
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(afd.getFileDescriptor());
}

Read mp3 metadata from assets folder

I have an app with some mp3 files stored inside a subfolder of assets folder.
I obtain list of files in this way
context.getAssets().list("audio");
and results show it inside a listview.
By doing this, what i see is a list of mp3 filenames. Now i don't want show filenames, but associated metadata for each file.
Copying the file to disk isn't necessary. MediaMetadataRetriever and FFmpegMediaMetadataRetriever both offer a setDataSource method that takes a FileDescriptor as an argument:
FFmpegMediaMetadataRetriever metaRetriver = new FFmpegMediaMetadataRetriever();\
// or MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever();
String path = "audio";
String [] files = context.getAssets().list(path);
for (int i = 0; i < files.length; i++) {
String file = path + "/" + files[i];
AssetFileDescriptor afd = context.getAssets().openFd(file);
metaRetriver.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
String album = metaRetriver.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
String artist = metaRetriver.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
String gener = metaRetriver.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_GENRE);
afd.close();
}
metaRetriver.release();
Here you go.
First get the file path as shown here
How to pass a file path which is in assets folder to File(String path)?
And then get metadata of audio as shown here
How to extract metadata from mp3?
By metadata means name, title, time etc data if you want then you need to use MediaMetadataRetriever class to do so..
MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource("path of song");
String album = metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
String artist = metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String gener=metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
Similarly you can extract other data also..
Hope it will help you.

Extract album cover from mp3 file in 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

Categories

Resources