Gathering Information Using MetaDataControl

MetaDataControl provides a simple way for Player instances to expose the meta information about media data. This meta information can be exposed with a set of predefined keys, such as Author, Title, Date, and Copyright, or the media may contain its own proprietary keys (with associated values).

As with StopTimeControl, not all media exposes this control. WAV and MP3 in sampled audio and MPEG in video are the most likely candidates that will provide meta information such as this. This doesn't mean that other media doesn't contain this information, just that the MMAPI implementation may not provide the MetaDataControl control for it.

Listing 8-3 shows you an example of using this control, where the information contained in an MP3 is displayed on the screen.

Listing 8-3. Using MetaDataControl to Display Meta Information package com.apress.chapter8;

import javax.microedition.midlet.*; import javax.microedition.lcdui.*;

import javax.microedition.media.*;

import javax.microedition.media.control.MetaDataControl;

public class MetaDataControlMIDlet extends MIDlet implements CommandListener {

  • define the display items private Display display = null; private List list = null; private Command exitCommand = null; private Alert alert = null;
  • the player instance private Player player = null;

public MetaDataControlMIDlet() {

// create the display items display = Display.getDisplay(this); alert = new Alert("Message");

exitCommand = new Command("Exit", Command.EXIT, 1);

  1. addCommand(exitCommand);
  2. setCommandListener(this);

list = new List("Message", List.IMPLICIT); list.addCommand(exitCommand);

list.setCommandListener(this);

// create and prefetch the player instance try {

player = Manager.createPlayer( getClass().getResourceAsStream( "/media/audio/chapter8/frogs.mp3"), "audio/mp3");

player.prefetch(); } catch(Exception e) { error(e);

public void startApp() {

// if player was created, extract control if(player != null) {

MetaDataControl mControl =

  • MetaDataControl)player.getControl( "javax.microedition.media.control.MetaDataControl");
  • if control is provided, show information onscreen if(mControl == null) {
  • no info alert.setString("No Meta Information"); display.setCurrent(alert); } else {
  • get all the keys of this control String[] keys = mControl.getKeys();
  • and append the key and its value to the list for(int i = 0; i < keys.length; i++) {

list.append(keys[i] + " -- " + mControl.getKeyValue(keys[i]), null);

// show the list display.setCurrent(list);

public void commandAction(Command cmd, Displayable disp) { if(cmd == exitCommand) { notifyDestroyed();

public void destroyApp(boolean unconditional) { }

// general purpose error method, displays onscreen as well to output private void error(Exception e) { alert.setString(e.getMessage()); alert.setTitle("Error"); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); e.printStackTrace();

As you can see, to get a list of keys, you use the method getKeys(), which returns a String array. There are four predefined keys in the MetaDataControl class: AUTHOR_KEY, TITLE_KEY, COPYRIGHT_KEY, and DATE_KEY. After you have the keys, whether predefined or retrieved using the getKeys() method, you can use the getKeyValue() method to get the value assigned to each key.

Figure 8-5 shows this MIDlet running in the Motorola C975 device, with only the title key showing a value. The Motorola C975 emulator doesn't support the playback of MP3 audio.

Figure 8-5. Using MetaDataControl to display meta information

0 0

Post a comment

  • Receive news updates via email from this site