Android: How to get audio detail from audio file

Libin picture Libin · Oct 27, 2015 · Viewed 10k times · Source

I have my music app started from external applications using intent data as music file.

So I have the mp3 audio URI something like this

file:///storage/emulated/0/Music/Tamil/I%20(2014)/Ennodu%20Nee%20Irundhaal.mp3

how to get the audio details from URI ie, the Media.TITLE , Media.ALBUM , Media._ID

Answer

Sachin Mandhare picture Sachin Mandhare · Oct 27, 2015

MediaMetaDataRetriever class: - MediaMetaDataRetriever class in android have many advantageous features to work on audio files. Its package name is “android.media.MediaMetadataRetriever” It is able to give predefined information of such files like:

  • Artist of song
  • Album name of song
  • Album art of song
  • Genre of song
  • Composer of song and many more options it has.

    MediaMetadataRetriever metaRetriver;
    metaRetriver = new MediaMetadataRetriever();
    metaRetriver.setDataSource("/sdcard/audio.mp3");
    

    The above code represent how to create object of MediaMetadataRetriever class and how to set the data source.

As in this code absolute path of audio file which is set of file which is in sd-card.

byte[] art;
art = metaRetriver.getEmbeddedPicture();

The above code is used to get album art in byte format from audio file.

Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);

The above code is used to convert metadata in byte form to Bitmap format so it can be easy to set on ImageView that is defined to show it.

All Code

ImageView album_art;
TextView album, artist, genre;

MediaMetadataRetriever metaRetriver;
byte[] art;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getInit();

    // Ablum_art retrieval code //

    metaRetriver = new MediaMetadataRetriever();
    metaRetriver.setDataSource("/sdcard/audio.mp3");
    try {
        art = metaRetriver.getEmbeddedPicture();
        Bitmap songImage = BitmapFactory
                .decodeByteArray(art, 0, art.length);
        album_art.setImageBitmap(songImage);
        album.setText(metaRetriver
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
        artist.setText(metaRetriver
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
        genre.setText(metaRetriver
                .extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
    } catch (Exception e) {
        album_art.setBackgroundColor(Color.GRAY);
        album.setText("Unknown Album");
        artist.setText("Unknown Artist");
        genre.setText("Unknown Genre");
    }

}

// Fetch Id's form xml 

public void getInit() {

    album_art = (ImageView) findViewById(R.id.album_art);
    album = (TextView) findViewById(R.id.Album);
    artist = (TextView) findViewById(R.id.artist_name);
    genre = (TextView) findViewById(R.id.genre);

}

main.xml

<? xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/album_art_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="60dp"
    android:text="Album Art"
    android:textSize="18dp" />

<ImageView
    android:id="@+id/album_art"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_marginRight="10dp" />

<TextView
    android:id="@+id/album_name_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/album_art_text"
    android:layout_below="@+id/album_art"
    android:layout_marginTop="24dp"
    android:text="Album Name :"
    android:textSize="18dp" />

<TextView
    android:id="@+id/artist_name_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/album_name_text"
    android:layout_below="@+id/album_name_text"
    android:layout_marginTop="43dp"
    android:text="Artist Name :"
    android:textSize="18dp" />

<TextView
    android:id="@+id/genre_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/artist_name_text"
    android:layout_below="@+id/artist_name_text"
    android:layout_marginTop="39dp"
    android:text="Genre :"
    android:textSize="18dp" />

<TextView
    android:id="@+id/genre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/genre_text"
    android:layout_alignBottom="@+id/genre_text"
    android:layout_toRightOf="@+id/album_art_text"
    android:text="null"
    android:textSize="18dp" />


<TextView
    android:id="@+id/Album"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/artist_name_text"
    android:layout_alignLeft="@+id/album_art"
    android:text="null"
    android:textSize="18dp" />

<TextView
    android:id="@+id/artist_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/genre_text"
    android:layout_alignLeft="@+id/Album"
    android:text="null"
    android:textSize="18dp" />

</RelativeLayout>