1

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.

giozh
  • 9,868
  • 30
  • 102
  • 183

3 Answers3

3

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();
William Seemann
  • 3,440
  • 10
  • 44
  • 78
0

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?

Community
  • 1
  • 1
Noman Rafique
  • 3,735
  • 26
  • 29
  • but in this way i copy file from asset folder to another folder into device storage, occupying double disk space. So if i have 20 MB of mp3 inside assets, after copy i occupy another 20 mb of storage, and i would avoid this – giozh Jan 25 '15 at 10:14
  • After getting metadata delete the file and pick next one and so one ;-) – Noman Rafique Jan 25 '15 at 10:23
  • This isn't the best approach, see my answer. – William Seemann Jan 26 '15 at 21:53
0

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.

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56