Rabu, 20 Agustus 2008

Multimedia in java me

to begin writing these code yaou have to read "Bismillah" to make you understand the code easyly

here is the main class of the project

package balengku;

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


/**
* @author glaShSial
*/
public class Saefan extends MIDlet implements CommandListener{
private Command cmdExit;
private Command cmdPlay;

private Display display;
private TextField textField;
private Gauge gauge;
public Form form;

private static final int GAUGE_LEVELS = 4;
private static final int GAUGE_MAX = 12;
private static final String DEFAULT_URL ="http://localhost/23Y132.mpg";

public Saefan() {
display = Display.getDisplay(this);
form = new Form("DoraMPEGPlayer");
textField = new TextField("MPEG Video URL", DEFAULT_URL, 100,TextField.ANY);
gauge = new Gauge("Loading MPEG Video", false, GAUGE_MAX, 0);
cmdExit = new Command("Exit", Command.EXIT, 2);
cmdPlay = new Command("Play", Command.SCREEN, 1);
form.addCommand(cmdPlay);
form.addCommand(cmdExit);
form.setCommandListener(this);
form.append(textField);
}

public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
//throw new UnsupportedOperationException("Not supported yet.");
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
} else if(c == cmdPlay) {
gauge.setValue(0);
form.append(gauge);
Rukzam mpegCanvas = new Rukzam(this);
mpegCanvas.initializeVideo(textField.getString());
}
}

public void updateGauge(){
int current = gauge.getValue();
current = (current + GAUGE_MAX/GAUGE_LEVELS);
gauge.setValue(current);
}
}

class Saefan was the main class of the project
so you have to write the code bellow to make the new class for the next class where it was named saefan too

here is the code:


package balengku;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;

/**
*
* @author glaShSial
*/
public class Rukzam extends Canvas implements CommandListener,PlayerListener,Runnable{
private Saefan parent;
private Display display;

private Player player;
private VideoControl videoControl;

private String url;
private Thread initializer;

private Command btnClose;
private Command btnReplay;

public Rukzam(Saefan parent) {
super();
this.parent = parent;
display = Display.getDisplay(parent);
btnClose = new Command("Close", Command.SCREEN, 1);
addCommand(btnClose);
setCommandListener(this);
}

public void initializeVideo(String url){
this.url = url;
initializer = new Thread(this);
initializer.start();
}

protected void paint(Graphics g) {
//throw new UnsupportedOperationException("Not supported yet.");
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
}

public void commandAction(Command c, Displayable d) {
//throw new UnsupportedOperationException("Not supported yet.");
if (c == btnReplay) {
try{
player.start();
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown",
me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
}
} else if (c == btnClose) {
player.close();
parent.form.delete(1);
display.setCurrent(parent.form);
url = null;
parent = null;
}

}

public void playerUpdate(Player p, String event, Object eventData) {
//throw new UnsupportedOperationException("Not supported yet.");
if (event == PlayerListener.END_OF_MEDIA) {
if (btnReplay == null) {
btnReplay = new Command("Replay", Command.SCREEN, 1);
addCommand(btnReplay);
}
}
}

public void run() {
//throw new UnsupportedOperationException("Not supported yet.");
try {
player = Manager.createPlayer(url);
parent.updateGauge();
player.addPlayerListener(this);
player.realize();
parent.updateGauge();
player.prefetch();
parent.updateGauge();
} catch (IOException ioe) {
Alert alert = new Alert("IOException thrown", ioe.getMessage(),
null, AlertType.ERROR);
display.setCurrent(alert);
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(),
null, AlertType.ERROR);
display.setCurrent(alert);
}
playVideo();
}

public void playVideo() {
try {
/* ambil video control dan set menjadi display sedang aktif */
videoControl = (VideoControl)player.getControl("VideoControl");
if (videoControl != null) {
videoControl.initDisplayMode(videoControl.USE_DIRECT_VIDEO,
this);
}

parent.updateGauge();
int cHeight = this.getHeight();
int cWidth = this.getWidth();
videoControl.setDisplaySize(cWidth, cHeight);
display.setCurrent(this);
videoControl.setVisible(true);
player.start();
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(),
null, AlertType.ERROR);
display.setCurrent(alert);
}
}



}


Good luck and enjoy the code....
e-mail me here saerukzam@yahoo.co.id or saefan@amikom.ac.id or to my YM: saerukzam@yahoo.co.id if you get some mistakes wich you don't undestand

Tidak ada komentar: