Home > Tutorials > Query MP3 Information using Android supported function

Query MP3 Information using Android supported function

That was quite a hard time to look for a way to retrieve MP3 metadata (header info) in Android. Mr. Google this time also doesn’t work for me _ _!

After a while looking around at Android Developers’ Reference, I’ve found one interesting class: MediaMetadataRetriever class, look at here. It seems like doing the function of retrieving music file header, like MP3 header information. So I give it a try and this is result:

Results from MediaMetadataRetriever

Results from MediaMetadataRetriever

It really looks good as you see! It’s better than the handy one in my previous post on getting MP3 basic information.

Give it a try:

package pete.android.study;

import android.app.Activity;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        TextView tvMain = (TextView)findViewById(R.id.tvMain);
        // load data file
        MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();

        String out = "";
        // get mp3 info
        out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
        out += "\n";
        out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        out += "\n";
        out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
        out += "\n";
        out += metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
        out += "\n";
        // convert duration to minute:seconds
        String duration = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long dur = Integer.parseInt(duration);
        String seconds = String.valueOf(dur % 60);
        String minutes = String.valueOf(dur / 60000);
        out += "Length: [ " + minutes + "m" + seconds + "s ]\n";

        // close object
        // display output

I’m quite surprised because of the result by using MediaMetadataRetriever, so simple and effective; also, just cannot believe that I cannot find any article on Internet about using this for querying Media or MP3 information.
I guess this article probably helps many of you in difficulties.
Hope you like it!

Browse source code from Android-Newbie Repository

Pete Houston

  1. lornelaliberte
    March 27, 2012 at 4:59 am

    Just thought I’d point out an error in your seconds calculation. (You’re actually calculating thousandths of seconds.)

    Instead of this:

    String seconds = String.valueOf(dur % 60);

    …what you want is this:

    String seconds = String.valueOf((dur % 60000) / 1000);

  2. October 20, 2011 at 11:49 pm

    Did the trick
    Bought a Tab, so don’t need to push data into an emulator anymore.
    Much easier and faster to debug on real device.

  3. October 19, 2011 at 10:34 am

    super new bee at Android (just started today)
    what does the res>layout xml look like for the MediaMetadataRetriever example.
    I’m getting an error for id on (TextView)findViewById(R.id.tvMain)

    And how do your push a mp3 music file in to the folder (“/sdcard/music.mp3”) of the emulator?

    • October 19, 2011 at 11:22 pm

      Just started then learn the basics before doing anything else, please. Anyway, source code updated including in the post.

      Have fun!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: