Home J2me J2ME Enumeration Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

J2ME Enumeration Example

Advertisement
Enumeration is an Interface that comes with the java.util package that can be implement by an object.

J2ME Enumeration Example

     

Enumeration is an Interface that comes with the java.util package that can be implement by an object. After going through the example you will be able to run a video file using interface enumeration in J2ME.

Interface Enumeration Example

Source Code of EnumrationMIDlet.java

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class EnumrationMIDlet extends MIDlet implements 
CommandListener, PlayerListener {     private Display display;
  private List itemList;
  private Form form;
  private Command stop, pause, start;
  private Hashtable items, itemsInfo;
  private Player player;

  public EnumrationMIDlet() {
  display = Display.getDisplay(this);
  itemList = new List("Enumration Example", List.IMPLICIT);
  stop = new Command("Stop", Command.STOP, 1);
  pause = new Command("Pause", Command.ITEM, 1);
  start = new Command("Start", Command.ITEM, 1);
  form = new Form("Playing video");
  form.addCommand(stop);
  form.addCommand(pause);
  form.setCommandListener(this);
  items = new Hashtable();
  itemsInfo = new Hashtable();

  items.put("SpringWaterFall...", "file://SpringWaterFall.mpg");
  itemsInfo.put("SpringWaterFall...", "video/mpeg");
  }

  public void startApp() {
  for(Enumeration en = items.keys(); en.hasMoreElements();) {
  itemList.append((String)en.nextElement(), null);
  }
  itemList.setCommandListener(this);
  display.setCurrent(itemList);
  }

  public void pauseApp() {
  try {
  if(player != null) player.stop();
  } catch(Exception e) {}
  }

  public void destroyApp(boolean unconditional) {
  if(player != null) player.close();
  }

  public void commandAction(Command c, Displayable d){
  if(d instanceof List) {
  List list = ((List)d);  
  String key = list.getString(list.getSelectedIndex());
  try {
  playAudio((String)items.get(key), key);
  } catch (Exception e) {
  System.err.println("Unable to play: " + e);
  e.printStackTrace();
  }
  } else if(d instanceof Form){
  try {
  if(c == stop){
  player.close();
  display.setCurrent(itemList);
  form.removeCommand(start);
  form.addCommand(pause);
  } else if(c == pause){
  player.stop();
  form.removeCommand(pause);
  form.addCommand(start);
  } else if(c == start){
  player.start();
  form.removeCommand(start);
  form.addCommand(pause);
  }
  } catch(Exception e) {
  System.err.println(e);
  }
  }
  }

  private void playAudio(String locator, String key) 
throws Exception { 
  String file = locator.substring(locator.indexOf
("file://") + 6, locator.length()); 
  player = Manager.createPlayer(getClass().getResourceAsStream(file), 
(String)itemsInfo.get(key)); 
  player.addPlayerListener(this);
  player.setLoopCount(-1);
  player.prefetch();
  player.realize();
  player.start();
  }

  public void playerUpdate(Player player, String event, Object eventData) {
  if(event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long)eventData)) {
  VideoControl vc = null;
  if((vc = (VideoControl)player.getControl("VideoControl")) != null) {
  Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
  form.append(videoDisp);
  }
  display.setCurrent(form);
  } else if(event.equals(PlayerListener.CLOSED)) {
  form.deleteAll();
  }
  }
}

Download Source Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2008

Ask Questions?    Discuss: J2ME Enumeration Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DaRk
June 17, 2011
pls canu help me out?

Can we create a code for displaying car shape in numbers
DMCA.com