j2me video streaming

hi all,
i am looking for an example/source code or a link about video streaming in j2me. although i have searched in google, not found a useful sample.

thanks in advance.

komputerAsked:
Who is Participating?
 
komputerAuthor Commented:
i fixed voice problem, the only thing left is fullscreen problem.
0
 
keyurkarnikCommented:
You should use the MMAPI of J2ME for this purpose. Its very simple to use.
0
 
keyurkarnikCommented:
Try this piece of code :

It works just fine.
Compile it using Eclipse and MTJ (Mobile Tools for JAVA) or whichever tools you are comfortable with.
import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MMMidlet extends MIDlet implements CommandListener
{

   private Display display;
   private List itemList;
   private Form form;

   private Hashtable items;

   public MMMidlet()
   {

      display = Display.getDisplay(this);

     
      itemList = new List("Select an item to play", List.IMPLICIT);


      // a form to display when items are being played
      form = new Form("Playing media");

     
      items = new Hashtable();

      
      items.put("Sample Video from Web", "http://home.in.tum.de/~paula/mpeg/wg_gdo_1.mpg");
      //items.put("Video 2", "http://link.to.video2"); //add more items here
   }

   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()
   {
   }

   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));
         }
         catch (Exception e)
         {
            System.err.println("Unable to play: " + e);
            e.printStackTrace();
         }
      }

   }


   /* Creates Player and plays media for the first time */
   private void playMedia(String locator) throws Exception
   {

      PlayerManager manager = new PlayerManager(form, itemList, locator, display);
      form.setCommandListener(manager);
      Thread runner = new Thread(manager);
      runner.start();
   }

   /* 
    * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
    * 
    * destroyApp
    * @param unconditional
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }
}

class PlayerManager implements Runnable, CommandListener, PlayerListener
{

   Form form;
   List list;
   Player player;
   String locator;
   Display display;

   private Command stopCommand;
   private Command pauseCommand;
   private Command startCommand;

   public PlayerManager(Form form, List list, String locator, Display display)
   {
      this.form = form;
      this.list = list;
      this.locator = locator;
      this.display = display;

      
      stopCommand = new Command("Stop", Command.STOP, 1);
      pauseCommand = new Command("Pause", Command.ITEM, 1);
      startCommand = new Command("Start", Command.ITEM, 1);

     
      form.addCommand(stopCommand);
      form.addCommand(pauseCommand);
   }

   public void run()
   {

      try
      {
         
         Alert alert = new Alert("Loading. Please wait ....");
         alert.setTimeout(Alert.FOREVER);
         display.setCurrent(alert);

         player = Manager.createPlayer(locator);

        
         player.addPlayerListener(this);

         player.setLoopCount(-1);
         player.prefetch();
         player.realize(); 

         player.start(); 
      }
      catch (Exception e)
      {
         System.err.println(e);
         e.printStackTrace();
      }
   }

   public void commandAction(Command command, Displayable disp)
   {
      if (disp instanceof Form)
      {
         
         try
         {
            if (command == stopCommand)
            { 
               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)
            { 
               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)
            { 
               player.start(); // starts from where the last pause was called
               form.removeCommand(startCommand);
               form.addCommand(pauseCommand);
            }
         }
         catch (Exception e)
         {
            System.err.println(e);
         }
      }
   }

  
   public void playerUpdate(Player player, String event, Object eventData)
   {

     
      if (event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long) eventData))
      {

        
         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
      }
   }

}

Open in new window

0
 
keyurkarnikCommented:
And here is a link for you to learn some more :

http://developers.sun.com/mobility/midp/articles/mmapioverview/
0
 
komputerAuthor Commented:
hi keyurkarnik,
in fact i had found familar examples on
http://today.java.net/pub/a/today/2005/09/27/j2me4.html
and other sides. they work well on wtk emulator but when i deploy in nokia n73 video plays  but voice is not played and fullscreen is failed either. i tried a lot examples but not works.

do you have any iade about this problems?

thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.