import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
import java.io.InputStream;
public class VideoPlayer extends MIDlet
implements CommandListener, Runnable, PlayerListener {
private Display display;
private Form form;
private Thread thread;
private Player player;
private VideoControl videocontrol;
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
private final Command cmdMainkan =
new Command("Mainkan", Command.SCREEN, 1);
private final Command cmdBerhenti =
new Command("Berhenti", Command.OK, 1);
public VideoPlayer() {
display = Display.getDisplay(this);
}
public void startApp() {
form = new Form("Video Player");
form.addCommand(cmdKeluar);
form.addCommand(cmdMainkan);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdMainkan) {
form.deleteAll();
thread = new Thread(this);
thread.start();
form.removeCommand(cmdMainkan);
form.removeCommand(cmdKeluar);
form.addCommand(cmdBerhenti);
} else if (c == cmdBerhenti) {
try {
player.stop();
stopVideo();
} catch (MediaException me) {
me.printStackTrace();
}
}
}
public void run() {
try {
InputStream is =
getClass().getResourceAsStream("Video.3gp");
player = Manager.createPlayer(is, "video/3gpp");
player.addPlayerListener(this);
player.realize();
// mendapatkan kontrol video
videocontrol =
(VideoControl) player.getControl("VideoControl");
// mendapatkan GUI untuk memainkan video
Item videoItem = (Item) videocontrol.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE, null);
// memasukkan item yang didapat ke dalam form
form.append(videoItem);
// menjalankan video
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopVideo() {
try {
player.deallocate();
player.close();
player = null;
thread = null;
form.removeCommand(cmdBerhenti);
form.addCommand(cmdMainkan);
form.addCommand(cmdKeluar);
} catch (Exception e) {
e.printStackTrace();
}
}
public void playerUpdate(Player player,
String event, Object data) {
if (event.equals(STOPPED) ||
event.equals(STOPPED_AT_TIME) ||
event.equals(ERROR) ||
event.equals(END_OF_MEDIA)) {
stopVideo();
}
}
}
No comments:
Post a Comment