02 Juli 2009

TUGAS BESAR JAVA

import java.util.Hashtable; import java.util.Enumeration;


import javax.microedition.lcdui.Item; import javax.microedition.lcdui.List; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.CommandListener?;

import javax.microedition.media.Player; import javax.microedition.media.Control; import javax.microedition.media.Manager; import javax.microedition.media.PlayerListener?; import javax.microedition.media.control.VideoControl?;

public class PlayVideo? extends MIDlet

implements CommandListener?, PlayerListener? {

private Display display; private List itemList; private Form form;

private Command stopCommand; private Command pauseCommand; private Command startCommand;

private Hashtable items; private Hashtable itemsInfo;

private Player player;

public PlayVideo?() {

display = Display.getDisplay(this);

itemList = new List("Video", List.IMPLICIT);

stopCommand = new Command("Stop", Command.STOP, 1); pauseCommand = new Command("Pause", Command.ITEM, 1); startCommand = new Command("Mulai", Command.ITEM, 1);

form = new Form("Play Video");

form.addCommand(stopCommand); form.addCommand(pauseCommand); form.setCommandListener(this);

items = new Hashtable();

itemsInfo = new Hashtable();

items.put("video.mpeg", "file://video.mpg"); itemsInfo.put("video.mpeg", "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 command, Displayable disp) {

if(disp instanceof List) {

List list = ((List)disp);

String key = list.getString(list.getSelectedIndex());

try {

playMedia((String)items.get(key), key);

} catch (Exception e) { }

} else if(disp instanceof Form) {

try {

if(command == stopCommand) {

player.close(); display.setCurrent(itemList); form.removeCommand(startCommand); form.addCommand(pauseCommand);

} else if(command == pauseCommand) {

player.stop(); form.removeCommand(pauseCommand); form.addCommand(startCommand);

} else if(command == startCommand) {

player.start(); form.removeCommand(startCommand); form.addCommand(pauseCommand);

}

} catch(Exception e) { }

}

}

private void playMedia(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();

}

}

}

Tidak ada komentar:

Posting Komentar