Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Extract Meta-data from Media file in Android

In this topic we will cover how to extract meta-data from existing audio file that will reside in sd-card.

Classes that we will need to perform this task:

  • MediaMetaDataRetriever
  • Bitmap
  • BitmapFactory

Knowledgeable Facts:

  • Understanding of MediaMetadataRetriever class.
  • Extraction of Album art, Album name, Artist name, Genre from mp3 file.
  • Conversion of Byte array to Bitmap for showing Album art.

Introduction to Classes used:

MediaMetadataRetriever: This class is used to extract metadata from media file. It comes from “android.media.MediaMetadataRetriever” package. It has many of available metadata types. A developer can use this class as per need. Here is the below some metadata types that we can extract from a media file in android.

  • METADATA_KEY_ALBUM : This key of metadata is used to extract the information about the Album associated with media file.
  • METADATA_KEY_ALBUMARTIST : This key of metadata is used to extract the information about the Artist or Performer associated with media file.
  • METADATA_KEY_ARTIST : This key of metadata is used to extract the information about the Artist associated with media file.
  • METADATA_KEY_AUTHOR : This key of metadata is used to extract the information about the author associated with media file.
  • METADATA_KEY_COMPOSER : This key of metadata is used to extract the information about the Composer associated with media file.
  • METADATA_KEY_BITRATE : This key of metadata is used to extract the information about the Bitrate associated with media file.
  • METADATA_KEY_GENRE: This key of metadata is used to extract the information about the Genre associated with media file.

Methods available in MediaMetadataRetriever class:

  1. void setDataSource(String path) : This method is used to set the media file source path in string format.
  2. string extractMetadata(int keyCode) : This method is used to extract metadata as per keys selected that are mentioned above.
  3. byte[] getEmbeddedPicture() : This method is used to extract album art associated with media file.

Bitmap Class: Bitmap class is basically used to work on Images. Bitmap class has the following useful method.

createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) : This method is used to create scaled bitmap from existing bitmap with specified width, height.

Here src defines the Bitmap source, dstWidth defines that width specified, dstHeight defines that height specified and filter defines whether source is filtered or not.

BitmapFactory Class : This class is very useful to work with decoding of byte array, files. It contains the following useful method.

decodeByteArray(byte[] data, int offset, int length) : This method is used to decode the byte array and convert it into Bitmap.

Here data defines the object of byte array, offset defines the starting point from where decoding has to start and length indicates the total length till array has to decode.

Steps involved: Here is below the steps that are involved in this project.

Step 1:

Create a new project in Eclipse with name MetaExtract and package name should be “com.nkm.metaextract”

Creating a new project Creating a new project 2

Figure 1: Creating a new project

Step 2:

Listing 1: Paste the following code in main.xml file.

<? 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>

Note: After pasting main.xml in UI mode will be as below:

main.xml in UI mode

Figure 2: main.xml in UI mode

Step 3:

Listing 3: Paste the following code into your MetaExtractActivity.java

package com.nkm.metaextract;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MetaExtractActivity extends Activity {
	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 reterival 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);

	}
}

Step 4:

Listing 4: Run your program it will give the following output.

Output of the program

Figure 3: Output of the program

Explanation of above code:

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.

Listing 5: Converting metadata in byte form

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");
		}

Explanation of above code:

  • In the above code try catch block is used to handle the runtime exceptions like if there is no metadata available for a particular media file then application will not force close due to this block.
  • album.setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); (This line will get the Album name of media file and set it on the album TextView ).
  • artist.setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); (This line will get the Artist name of media file and set it on the artist TextView).
  • setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)); (This line will get the Genre name of media file and set it on the genre TextView).
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);

	}

Explanation of above code:

This is user defined method which is used to fetch ids from xml into java file.

What is covered?

  1. Introduction to metadata in android
  2. How to use MediaMetadataRetriever class , Bitmap class and BitmapFactory class.
  3. How to use BitmapFactory class to decode byte array for album art extraction.
  4. Different types of Metadata keys for extraction of Metadata of media file.


Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login