Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import javax.microedition.media.*;
- import javax.microedition.media.control.VideoControl;
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import com.wingfoot.soap.*;
- import com.wingfoot.soap.transport.*;
- import com.wingfoot.soap.encoding.*;
- import java.util.*;
- /**
- * @author agoesaza
- */
- public class MidletMPWS extends MIDlet implements CommandListener {
- private Display display;
- private Display display2;
- private TextField userName;
- private TextField password;
- private Form form;
- private Form form2;
- private Form form3;
- private Command cancel;
- private Command login;
- private Command logout;
- private List itemList;
- private Hashtable items;
- private String url = "http://localhost/testWS/testWS.php";
- public MidletMPWS() {
- userName = new TextField("Username :", "", 10, TextField.ANY);
- password = new TextField("Password :", "", 10, TextField.PASSWORD);
- form = new Form("Login");
- login = new Command("Login", Command.OK, 2);
- cancel = new Command("Cancel", Command.CANCEL, 2);
- logout = new Command("Logout", Command.OK , 2);
- display2 = Display.getDisplay(this);
- itemList = new List("Select an item to play", List.IMPLICIT);
- form2 = new Form("Playing media");
- }
- public void startApp() {
- display = Display.getDisplay(this);
- form.setTitle("Login");
- form.append(userName);
- form.append(password);
- form.addCommand(login);
- form.addCommand(cancel);
- form.setCommandListener(this);
- display.setCurrent(form);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- notifyDestroyed();
- }
- public void validateUser(String name, String password) throws Exception {
- Call c = new Call();
- c.setMethodName("login");
- c.addParameter("username", name);
- c.addParameter("password", password);
- HTTPTransport transport = new HTTPTransport (url,null);
- transport.getResponse(true);
- Envelope res=c.invoke(transport);
- if (res.getParameter(0).toString().equals("1")) {
- bener(name);
- } else {
- tryAgain();
- }
- }
- public void bener(String name) throws Exception {
- Alert benar = new Alert("Login Sukses", "Selamat Login Sukses", null, AlertType.ERROR);
- benar.setTimeout(Alert.FOREVER);
- display.setCurrent(benar);
- Call c = new Call();
- c.setMethodName("getKoleksiID");
- c.addParameter("username", name);
- HTTPTransport transport = new HTTPTransport (url,null);
- transport.getResponse(true);
- Envelope res=c.invoke(transport);
- //UntypedObject o=(UntypedObject)res.getParameter(0);
- Object[] o = (Object[])res.getParameter(0);
- //if (o!=null) {
- itemList.deleteAll();
- items = new Hashtable();
- for(int i=0;i<o.length;i++){
- //items.put(o.getPropertyValue(1).toString(), "aaaaaa");
- Call b = new Call();
- b.setMethodName("getKoleksi");
- b.addParameter("noFile", o[i].toString());
- HTTPTransport trans = new HTTPTransport (url,null);
- trans.getResponse(true);
- Envelope response=b.invoke(trans);
- UntypedObject uo=(UntypedObject)response.getParameter(0);
- if (uo!=null) {
- items.put(uo.getPropertyValue(2) + " - " + uo.getPropertyValue(1),uo.getPropertyValue(0));
- }
- }
- items.put("Video","http://localhost/testWS/File/12.mpg");
- for(Enumeration en = items.keys(); en.hasMoreElements();) {
- itemList.append((String)en.nextElement(), null);
- }
- itemList.setCommandListener(this);
- itemList.addCommand(logout);
- display.setCurrent(itemList);
- //}
- }
- public void tryAgain() {
- Alert salah = new Alert("Login salah", "Silakan Ulangi lagi", null, AlertType.ERROR);
- salah.setTimeout(Alert.FOREVER);
- userName.setString("");
- password.setString("");
- display.setCurrent(salah, form);
- }
- public void commandAction(Command c, Displayable d) {
- String label = c.getLabel();
- if(label.equals("Cancel")) {
- destroyApp(true);
- } else if(label.equals("Login")) {
- try {
- validateUser(userName.getString(), password.getString());
- } catch (Exception ex) {
- tryAgain();
- }
- } else if(label.equals("Logout")) {
- userName.setString("");
- password.setString("");
- display.setCurrent(form);
- } else {
- if(d instanceof List) {
- List list = ((List)d);
- String key = list.getString(list.getSelectedIndex());
- try {
- playMedia((String)items.get(key));
- } catch (Exception e) {
- System.err.println("Unable to play: " + e);
- // e.printStackTrace();
- }
- }
- }
- }
- private void playMedia(String noFile) throws Exception {
- PlayerManager manager =
- new PlayerManager(form2, itemList, noFile, display2);
- form2.setCommandListener(manager);
- Thread runner = new Thread(manager);
- try {
- runner.start();
- } catch (Exception e) {
- Alert exMessages = new Alert("Media Player", e.toString(), null, AlertType.ERROR);
- exMessages.setTimeout(Alert.FOREVER);
- display.setCurrent(exMessages, form);
- }
- }
- }
- class PlayerManager implements Runnable, CommandListener, PlayerListener {
- Form form;
- List list;
- Player player;
- String locator, noFile;
- Display display;
- private StringItem strResult;
- private Command stopCommand;
- private Command pauseCommand;
- private Command startCommand;
- private String url = "http://localhost/testWS/testWS.php";
- public PlayerManager(Form form, List list, String noFile, Display display) {
- this.form = form;
- this.list = list;
- this.noFile = noFile;
- this.display = display;
- strResult = new StringItem("","");
- // stop, pause and restart commands
- stopCommand = new Command("Stop", Command.STOP, 1);
- pauseCommand = new Command("Pause", Command.ITEM, 1);
- startCommand = new Command("Start", Command.ITEM, 1);
- // the form acts as the interface to stop and pause the media
- form.append(strResult);
- form.addCommand(stopCommand);
- form.addCommand(pauseCommand);
- }
- public void run() {
- form.deleteAll();
- form.append(strResult);
- form.addCommand(stopCommand);
- form.addCommand(pauseCommand);
- try {
- Call b = new Call();
- b.setMethodName("getKoleksi");
- b.addParameter("noFile", noFile);
- HTTPTransport trans = new HTTPTransport (url,null);
- trans.getResponse(true);
- Envelope response=b.invoke(trans);
- UntypedObject uo=(UntypedObject)response.getParameter(0);
- // since we are loading data over the network, a delay can be
- // expected
- Alert alert = new Alert("Loading. Please wait ....");
- alert.setTimeout(Alert.FOREVER);
- display.setCurrent(alert);
- strResult.setText("Title : " + uo.getPropertyValue(2).toString() + "\nArtis : " + uo.getPropertyValue(1).toString());
- locator = uo.getPropertyValue(3).toString();
- VideoControl vc = null;
- defplayer();
- player = Manager.createPlayer(locator);
- player.addPlayerListener(this);
- player.realize(); // realize
- if((vc = (VideoControl)player.getControl("VideoControl")) != null) {
- Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
- form.append(videoDisp);
- }
- display.setCurrent(form);
- player.setLoopCount(-1); // play indefinitely
- player.prefetch(); // prefetch
- player.start(); // and start
- } catch(Exception e) {
- form.removeCommand(startCommand); // remove the start command
- form.removeCommand(pauseCommand); // remove the pause command
- form.removeCommand(stopCommand); // and the stop command
- Alert exMessages = new Alert("Media Player", e.toString(), null, AlertType.ERROR);
- exMessages.setTimeout(Alert.FOREVER);
- display.setCurrent(exMessages, list);
- }
- }
- public void commandAction(Command command, Displayable disp) {
- if(disp instanceof Form) {
- // if showing form, means the media is being played
- // and the user is trying to stop or pause the player
- try {
- if(command == stopCommand) { // if stopping the media play
- player.close(); // close the player
- display.setCurrent(list); // redisplay the list of media
- form.removeCommand(startCommand); // remove the start command
- form.removeCommand(pauseCommand); // remove the pause command
- form.removeCommand(stopCommand); // and the stop command
- } else if(command == pauseCommand) { // if pausing
- player.stop(); // pauses the media, note that it is called stop
- form.removeCommand(pauseCommand); // remove the pause command
- form.addCommand(startCommand); // add the start (restart) command
- } else if(command == startCommand) { // if restarting
- player.start(); // starts from where the last pause was called
- form.removeCommand(startCommand);
- form.addCommand(pauseCommand);
- }
- } catch(Exception e) {
- display.setCurrent(list); // redisplay the list of media
- form.removeCommand(startCommand); // remove the start command
- form.removeCommand(pauseCommand); // remove the pause command
- form.removeCommand(stopCommand); // and the stop command
- Alert exMessages = new Alert("Media Player", e.toString(), null, AlertType.ERROR);
- exMessages.setTimeout(Alert.FOREVER);
- display.setCurrent(exMessages, form);
- }
- }
- }
- /* Handle player events */
- public void playerUpdate(Player player, String event, Object eventData) {
- if(event == PlayerListener.END_OF_MEDIA) {
- try {
- defplayer();
- }
- catch(MediaException me) {
- }
- player = null;
- }
- }
- void defplayer() throws MediaException {
- if (player != null) {
- if(player.getState() == Player.STARTED) {
- player.stop();
- }
- if(player.getState() == Player.PREFETCHED) {
- player.deallocate();
- }
- if(player.getState() == Player.REALIZED ||
- player.getState() == Player.UNREALIZED) {
- player.close();
- }
- }
- player = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement