Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.24 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. import javax.microedition.media.*;
  6. import javax.microedition.media.control.VideoControl;
  7. import javax.microedition.midlet.*;
  8. import javax.microedition.lcdui.*;
  9. import com.wingfoot.soap.*;
  10. import com.wingfoot.soap.transport.*;
  11. import com.wingfoot.soap.encoding.*;
  12. import java.util.*;
  13. /**
  14.  * @author agoesaza
  15.  */
  16. public class MidletMPWS extends MIDlet implements CommandListener {
  17.     private Display display;
  18.     private Display display2;
  19.     private TextField userName;
  20.     private TextField password;
  21.     private Form form;
  22.     private Form form2;
  23.     private Form form3;
  24.     private Command cancel;
  25.     private Command login;
  26.     private Command logout;
  27.     private List itemList;
  28.  
  29.     private Hashtable items;
  30.     private String url = "http://localhost/testWS/testWS.php";
  31.    
  32.    
  33.     public MidletMPWS() {
  34.         userName = new TextField("Username :", "", 10, TextField.ANY);
  35.         password = new TextField("Password :", "", 10, TextField.PASSWORD);
  36.         form = new Form("Login");
  37.         login = new Command("Login", Command.OK, 2);
  38.         cancel =  new Command("Cancel", Command.CANCEL, 2);
  39.         logout = new Command("Logout", Command.OK , 2);
  40.        
  41.         display2 = Display.getDisplay(this);
  42.         itemList = new List("Select an item to play", List.IMPLICIT);
  43.         form2 = new Form("Playing media");
  44.     }
  45.  
  46.     public void startApp() {
  47.         display = Display.getDisplay(this);
  48.         form.setTitle("Login");
  49.         form.append(userName);
  50.         form.append(password);
  51.         form.addCommand(login);
  52.         form.addCommand(cancel);
  53.         form.setCommandListener(this);
  54.         display.setCurrent(form);
  55.     }
  56.    
  57.     public void pauseApp() {
  58.     }
  59.  
  60.     public void destroyApp(boolean unconditional) {
  61.         notifyDestroyed();
  62.     }
  63.  
  64.     public void validateUser(String name, String password) throws Exception {
  65.             Call c = new Call();
  66.             c.setMethodName("login");
  67.             c.addParameter("username", name);
  68.             c.addParameter("password", password);
  69.        
  70.             HTTPTransport transport = new HTTPTransport (url,null);
  71.             transport.getResponse(true);
  72.        
  73.             Envelope res=c.invoke(transport);
  74.        
  75.             if (res.getParameter(0).toString().equals("1")) {
  76.                 bener(name);  
  77.             } else {
  78.                 tryAgain();
  79.             }
  80.     }
  81.     public void bener(String name) throws Exception {
  82.         Alert benar = new Alert("Login Sukses", "Selamat Login Sukses", null, AlertType.ERROR);
  83.         benar.setTimeout(Alert.FOREVER);
  84.         display.setCurrent(benar);
  85.        
  86.             Call c = new Call();
  87.             c.setMethodName("getKoleksiID");
  88.             c.addParameter("username", name);
  89.        
  90.             HTTPTransport transport = new HTTPTransport (url,null);
  91.             transport.getResponse(true);
  92.        
  93.             Envelope res=c.invoke(transport);
  94.             //UntypedObject o=(UntypedObject)res.getParameter(0);
  95.             Object[] o = (Object[])res.getParameter(0);
  96.             //if (o!=null) {
  97.             itemList.deleteAll();
  98.             items = new Hashtable();
  99.             for(int i=0;i<o.length;i++){
  100.             //items.put(o.getPropertyValue(1).toString(), "aaaaaa");
  101.                 Call b = new Call();
  102.                 b.setMethodName("getKoleksi");
  103.                 b.addParameter("noFile", o[i].toString());
  104.                
  105.                 HTTPTransport trans = new HTTPTransport (url,null);
  106.                 trans.getResponse(true);
  107.                
  108.                 Envelope response=b.invoke(trans);
  109.                
  110.                 UntypedObject uo=(UntypedObject)response.getParameter(0);
  111.                
  112.                 if (uo!=null) {
  113.                 items.put(uo.getPropertyValue(2) + " - " + uo.getPropertyValue(1),uo.getPropertyValue(0));
  114.                 }
  115.             }
  116.             items.put("Video","http://localhost/testWS/File/12.mpg");
  117.             for(Enumeration en = items.keys(); en.hasMoreElements();) {
  118.                 itemList.append((String)en.nextElement(), null);
  119.             }
  120.             itemList.setCommandListener(this);
  121.             itemList.addCommand(logout);
  122.             display.setCurrent(itemList);
  123.             //}
  124.        
  125.     }
  126.  
  127.     public void tryAgain() {
  128.         Alert salah = new Alert("Login salah", "Silakan Ulangi lagi", null, AlertType.ERROR);
  129.         salah.setTimeout(Alert.FOREVER);
  130.         userName.setString("");
  131.         password.setString("");
  132.         display.setCurrent(salah, form);
  133.     }
  134.  
  135.     public void commandAction(Command c, Displayable d) {
  136.  
  137.        
  138.         String label = c.getLabel();
  139.         if(label.equals("Cancel")) {
  140.             destroyApp(true);
  141.         } else if(label.equals("Login")) {
  142.             try {
  143.               validateUser(userName.getString(), password.getString());
  144.             } catch (Exception ex) {
  145.                 tryAgain();
  146.             }
  147.         } else if(label.equals("Logout")) {
  148.             userName.setString("");
  149.             password.setString("");
  150.             display.setCurrent(form);
  151.         } else {
  152.             if(d instanceof List) {
  153.                 List list = ((List)d);
  154.                 String key = list.getString(list.getSelectedIndex());
  155.                 try {
  156.                     playMedia((String)items.get(key));
  157.                 } catch (Exception e) {
  158.                     System.err.println("Unable to play: " + e);
  159.                    // e.printStackTrace();
  160.                 }
  161.             }
  162.         }
  163.     }
  164.    
  165.     private void playMedia(String noFile) throws Exception {
  166.  
  167.     PlayerManager manager =
  168.       new PlayerManager(form2, itemList, noFile, display2);
  169.     form2.setCommandListener(manager);
  170.     Thread runner = new Thread(manager);
  171.     try {
  172.         runner.start();
  173.     } catch (Exception e) {
  174.         Alert exMessages = new Alert("Media Player", e.toString(), null, AlertType.ERROR);
  175.         exMessages.setTimeout(Alert.FOREVER);
  176.         display.setCurrent(exMessages, form);
  177.     }
  178.   }
  179. }
  180.  
  181. class PlayerManager implements Runnable, CommandListener, PlayerListener {
  182.  
  183.   Form form;
  184.   List list;
  185.   Player player;
  186.   String locator, noFile;
  187.   Display display;
  188.  
  189.   private StringItem strResult;
  190.   private Command stopCommand;
  191.   private Command pauseCommand;
  192.   private Command startCommand;
  193.  
  194.   private String url = "http://localhost/testWS/testWS.php";
  195.  
  196.   public PlayerManager(Form form, List list, String noFile, Display display) {
  197.   this.form = form;
  198.   this.list = list;
  199.   this.noFile = noFile;
  200.   this.display = display;
  201.  
  202.   strResult = new StringItem("","");
  203.     // stop, pause and restart commands
  204.     stopCommand = new Command("Stop", Command.STOP, 1);
  205.     pauseCommand = new Command("Pause", Command.ITEM, 1);
  206.     startCommand = new Command("Start", Command.ITEM, 1);
  207.  
  208.     // the form acts as the interface to stop and pause the media
  209.     form.append(strResult);
  210.     form.addCommand(stopCommand);
  211.     form.addCommand(pauseCommand);
  212.    
  213.   }
  214.  
  215.   public void run() {
  216. form.deleteAll();
  217. form.append(strResult);
  218.     form.addCommand(stopCommand);
  219.     form.addCommand(pauseCommand);
  220.     try {
  221.       Call b = new Call();
  222.       b.setMethodName("getKoleksi");
  223.       b.addParameter("noFile", noFile);
  224.                
  225.       HTTPTransport trans = new HTTPTransport (url,null);
  226.       trans.getResponse(true);
  227.       Envelope response=b.invoke(trans);
  228.       UntypedObject uo=(UntypedObject)response.getParameter(0);
  229.  
  230.       // since we are loading data over the network, a delay can be
  231.       // expected
  232.       Alert alert = new Alert("Loading. Please wait ....");
  233.       alert.setTimeout(Alert.FOREVER);
  234.       display.setCurrent(alert);
  235.       strResult.setText("Title : " + uo.getPropertyValue(2).toString() + "\nArtis : " + uo.getPropertyValue(1).toString());
  236.       locator = uo.getPropertyValue(3).toString();
  237.       VideoControl vc = null;
  238.       defplayer();
  239.       player = Manager.createPlayer(locator);
  240.  
  241.       player.addPlayerListener(this);
  242.       player.realize(); // realize
  243.       if((vc = (VideoControl)player.getControl("VideoControl")) != null) {
  244.         Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);
  245.         form.append(videoDisp);
  246.       }
  247.       display.setCurrent(form);
  248.       player.setLoopCount(-1); // play indefinitely
  249.       player.prefetch(); // prefetch
  250.  
  251.       player.start(); // and start
  252.     } catch(Exception e) {
  253.         form.removeCommand(startCommand); // remove the start command
  254.         form.removeCommand(pauseCommand); // remove the pause command
  255.         form.removeCommand(stopCommand);  // and the stop command
  256.         Alert exMessages = new Alert("Media Player", e.toString(), null, AlertType.ERROR);
  257.         exMessages.setTimeout(Alert.FOREVER);
  258.         display.setCurrent(exMessages, list);
  259.  
  260.     }
  261.   }
  262.  
  263.   public void commandAction(Command command, Displayable disp) {
  264.     if(disp instanceof Form) {
  265.       // if showing form, means the media is being played
  266.       // and the user is trying to stop or pause the player
  267.       try {
  268.         if(command == stopCommand) { // if stopping the media play
  269.           player.close(); // close the player
  270.           display.setCurrent(list); // redisplay the list of media
  271.           form.removeCommand(startCommand); // remove the start command
  272.           form.removeCommand(pauseCommand); // remove the pause command
  273.           form.removeCommand(stopCommand);  // and the stop command
  274.         } else if(command == pauseCommand) { // if pausing
  275.           player.stop(); // pauses the media, note that it is called stop
  276.           form.removeCommand(pauseCommand); // remove the pause command
  277.           form.addCommand(startCommand); // add the start (restart) command
  278.         } else if(command == startCommand) { // if restarting
  279.           player.start(); // starts from where the last pause was called
  280.           form.removeCommand(startCommand);
  281.           form.addCommand(pauseCommand);
  282.         }
  283.       } catch(Exception e) {
  284.           display.setCurrent(list); // redisplay the list of media
  285.           form.removeCommand(startCommand); // remove the start command
  286.           form.removeCommand(pauseCommand); // remove the pause command
  287.           form.removeCommand(stopCommand);  // and the stop command
  288.         Alert exMessages = new Alert("Media Player", e.toString(), null, AlertType.ERROR);
  289.         exMessages.setTimeout(Alert.FOREVER);
  290.         display.setCurrent(exMessages, form);
  291.       }
  292.     }
  293.   }
  294.  
  295.   /* Handle player events */
  296.   public void playerUpdate(Player player, String event, Object eventData) {
  297.     if(event == PlayerListener.END_OF_MEDIA) {
  298.         try {
  299.             defplayer();
  300.         }
  301.         catch(MediaException me) {
  302.         }
  303.         player = null;
  304.       }
  305.   }
  306.  
  307.      void defplayer() throws MediaException {
  308.       if (player != null) {
  309.          if(player.getState() == Player.STARTED) {
  310.             player.stop();
  311.          }
  312.          if(player.getState() == Player.PREFETCHED) {
  313.             player.deallocate();
  314.          }
  315.          if(player.getState() == Player.REALIZED ||
  316.             player.getState() == Player.UNREALIZED) {
  317.             player.close();
  318.          }
  319.       }
  320.       player = null;
  321.    }
  322.  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement