MetaData Control : mp3 « J2ME « Java Tutorial






import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.MetaDataControl;
import javax.microedition.midlet.MIDlet;

public class MetaDataControlMIDlet extends MIDlet implements CommandListener {
  private Display display = null;

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

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

  private Alert alert = new Alert("Message");

  private Player player = null;

  public MetaDataControlMIDlet() {
    display = Display.getDisplay(this);
    alert.addCommand(exitCommand);
    alert.setCommandListener(this);
    list.addCommand(exitCommand);
    list.setCommandListener(this);

    try {
      player = Manager.createPlayer(getClass().getResourceAsStream("/s.mp3"), "audio/mp3");
      player.prefetch();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void startApp() {
    if (player != null) {
      MetaDataControl mControl = (MetaDataControl) player
          .getControl("javax.microedition.media.control.MetaDataControl");

      if (mControl == null) {
        alert.setString("No Meta Information");
        display.setCurrent(alert);
      } else {
        String[] keys = mControl.getKeys();
        for (int i = 0; i < keys.length; i++) {
          list.append(keys[i] + " -- " + mControl.getKeyValue(keys[i]), null);
        }
        display.setCurrent(list);
      }
    }
  }

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

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

}








31.58.mp3
31.58.1.MetaData Control