×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    J2ME: Play videos

    Hello, i´m new in Java, and i need to play videos in Mobile. So, i read about that and i try the follow code:

    Code:
    package P2_ManagerPKG;
    
    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.control.VideoControl;
    import javax.microedition.media.Player;
    import javax.microedition.media.Control;
    import javax.microedition.media.Manager;
    import javax.microedition.media.PlayerListener;
    
    public class P2_mng  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 P2_mng () {
        display = Display.getDisplay(this);
        // creates an item list to let you select multimedia files to play
        itemList = new List("Select an item to play", List.IMPLICIT);
    
        // 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);
    
        // a form to display when items are being played
        form = new Form("Playing media");
    
        // the form acts as the interface to stop and pause the media
        form.addCommand(stopCommand);
        form.addCommand(pauseCommand);
        form.setCommandListener(this);
    
        // create a hashtable of items
        items = new Hashtable();
    
        // and a hashtable to hold information about them
        itemsInfo = new Hashtable();
    
        // and populate both of them
        items.put("Promo Video from jar", "file://localhost/E:/Videos/movie1.3gp");
        itemsInfo.put("Promo Video from jar", "video/3gpp");
      }
    
      public void startApp() {
    
        // when MIDlet is started, use the item list to display elements
        for(Enumeration en = items.keys(); en.hasMoreElements();) {
        itemList.append((String)en.nextElement(), null);
      }
    
        itemList.setCommandListener(this);
    
        // show the list when MIDlet is started
        display.setCurrent(itemList);
      }
    
      public void pauseApp() {
        // pause the player
        try {
          if(player != null) player.stop();
        } catch(Exception e) {}
      }
    
      public void destroyApp(boolean unconditional) {
        if(player != null) player.close(); // close the player
      }
    
      public void commandAction(Command command, Displayable disp) {
    
        // generic command handler
    
        // if list is displayed, the user wants to play the item
        if(disp instanceof List) {
          List list = ((List)disp);
    
          String key = list.getString(list.getSelectedIndex());
    
          // try and play the selected file
          try {
            playMedia((String)items.get(key), key);
          } catch (Exception e) {
            System.err.println("Unable to play: " + e);
            e.printStackTrace();
          }
        } else 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(itemList); // redisplay the list of media
              form.removeCommand(startCommand); // remove the start command
              form.addCommand(pauseCommand); // add the pause 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) {
            System.err.println(e);
          }
        }
    
      }
    
      /* Creates Player and plays media for the first time */
      private void playMedia(String locator, String key) throws Exception {
    
        
        // create the player
        // loading it as a resource and using information about it
        // from the itemsInfo hashtable
        player = Manager.createPlayer(getClass().getResourceAsStream("file://localhost/E:/Videos/movie1.3gp"), "video/3gpp");
    
        // a listener to handle player events like starting, closing etc
        player.addPlayerListener(this);
    
        player.setLoopCount(-1); // play indefinitely
        player.prefetch(); // prefetch
        player.realize(); // realize
    
        player.start(); // and start
      }
    
      /* Handle player events */
      public void playerUpdate(Player player, String event, Object eventData) {
    
        // if the event is that the player has started, show the form
    	// but only if the event data indicates that the event relates to newly
    	// stated player, as the STARTED event is fired even if a player is
    	// restarted. Note that eventData indicates the time at which the start
    	// event is fired.
    	if (event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long)eventData))
            {
            // see if we can show a video control, depending on whether the media
    		// is a video or not
    		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(); // clears the form of any previous controls
    	}
      }
    }
    The movie1.3gp is in the correct directory, and when i press "Choose" nothing happens in Mobile, but in the output window ( in netbeans) appears:

    "Series 40 Nokia 6212 NFC SDK" Instance #6212000 Ready for Future Connections
    at javax.microedition.media.Manager.createPlayer(Manager.java:228)
    at P2_ManagerPKG.P2_mng.playMedia(P2_mng.java:145)
    at P2_ManagerPKG.P2_mng.commandAction(P2_mng.java:102)
    at javax.microedition.lcdui.Display.consumeDisplayEvent(+731)
    at javax.microedition.lcdui.Display$DisplayAccessor.consumeEvent(+109)
    at com.nokia.mid.impl.isa.ui.EventDispatcher.run(EventDispatcher.java:354)
    Unable to play: java.lang.IllegalArgumentException: Param is null
    java.lang.IllegalArgumentException: Param is null


    Anyone can help me with this problem??

    Thank´s.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: J2ME: Play videos

    I feel the issue is with
    Code:
    player = Manager.createPlayer(getClass().getResourceAsStream("file://localhost/E:/Videos/movie1.3gp"), "video/3gpp");
    getResourceAsStream("file://localhost/E:/Videos/movie1.3gp") can be used only if the 3gp file is in the resource folder of your project i.e. its a part of your deployable JAR file that goes on the device.

    If you want to play something from localhost - server, you can directly pass the server URI to create the player.

    Player p = Manager.createPlayer("http://localhost/movie1.3gp");
    if this file is available on the webserver hosted on the localhost.
    Thanks,
    Ekta

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Re: J2ME: Play videos

    Quote Originally Posted by ektasrv View Post
    I feel the issue is with
    Code:
    player = Manager.createPlayer(getClass().getResourceAsStream("file://localhost/E:/Videos/movie1.3gp"), "video/3gpp");
    getResourceAsStream("file://localhost/E:/Videos/movie1.3gp") can be used only if the 3gp file is in the resource folder of your project i.e. its a part of your deployable JAR file that goes on the device.

    If you want to play something from localhost - server, you can directly pass the server URI to create the player.

    Player p = Manager.createPlayer("http://localhost/movie1.3gp");
    if this file is available on the webserver hosted on the localhost.
    Hi, you were right, that was the problem. I put the movie in res folder and put

    Code:
    player = Manager.createPlayer(getClass().getResourceAsStream("/res/movie1.3gp"), "video/3gpp");
    and now works fine.

    Thank you very much for helping me.

Similar Threads

  1. How can i play videos on my N97?
    By Dillen in forum Streaming and Video
    Replies: 2
    Last Post: 2010-10-02, 17:03
  2. What are the extensions to stream native videos and play device videos in FL 3.0?
    By supriyaakg in forum [Archived] Flash Lite on Nokia Devices
    Replies: 7
    Last Post: 2010-04-16, 06:34
  3. Cannot Play Videos on E71
    By p1LL in forum Streaming and Video
    Replies: 4
    Last Post: 2009-07-22, 07:54
  4. Play Videos on my 6085
    By NBK25 in forum Streaming and Video
    Replies: 1
    Last Post: 2009-04-28, 04:53
  5. Play two videos simultaneously
    By rithesh.pai in forum Symbian Media (Closed)
    Replies: 6
    Last Post: 2007-08-31, 09:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •