How to play video streaming in Java ME
Article Metadata
The simple example MIDlet displays on how to play RTSP video streaming in Java ME:
package example;
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.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Ticker;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;
public class VideoCanvas extends Canvas implements PlayerListener, CommandListener {
private ExampleStreamingMIDlet midlet = null;
private Command start = new Command("Start",Command.OK,0);
private Command stop = new Command("Stop",Command.OK,0);
private Command back = new Command("Back",Command.OK,0);
private Command exit = new Command("Exit",Command.BACK,0);
private String url = "rtsp://rtsp.youtube.com/youtube/videos/HtVbAazUekM/video.3gp";
private String status = "Press left softkey";
private String status2 = "";
private Player player = null;
private VideoControl control = null;
/**
* Constructor
*
* @param midlet
*/
public VideoCanvas(ExampleStreamingMIDlet midlet, String url) {
this.midlet = midlet;
this.url = null;
addCommand(start);
addCommand(stop);
addCommand(back);
addCommand(exit);
setCommandListener(this);
this.setFullScreenMode(true);
}
public void commandAction(Command c, Displayable arg1) {
if(c == start) {
start();
}
else if(c == stop) {
stop();
}
else if(c == exit) {
midlet.notifyDestroyed();
}
else if(c == back) {
Display.getDisplay(midlet).setCurrent(midlet.list);
}
}
/**
* Paint
*/
protected void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(status2,0,0,Graphics.LEFT|Graphics.TOP);
g.drawString(status,getWidth(),getHeight(),Graphics.RIGHT|Graphics.BOTTOM);
}
/**
* Start
*
*/
public void start() {
try {
player = Manager.createPlayer(url);
player.addPlayerListener(this);
player.realize();
//uncomment for video problem with prefetch();
//player.prefetch();
// Grab the video control and set it to the current display.
control = (VideoControl)player.getControl("VideoControl");
if (control != null) {
control.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,this);
control.setDisplaySize(176,144);
int width = control.getSourceWidth();
int height = control.getSourceHeight();
status2 = "Before: SW=" + width + "-SH=" + height + "-DW=" + control.getDisplayWidth() + "-DH=" + control.getDisplayHeight();
}
player.start();
}
catch(Exception e) {
Alert erro = new Alert("Erro",e.getMessage(),null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(erro);
}
}
public void stop() {
if(player != null) {
player.deallocate();
player.close();
}
}
public void playerUpdate(Player p, String s, Object o) {
status = s;
if(p.getState() == Player.STARTED) {
int width = control.getDisplayWidth();
int height = control.getDisplayHeight();
control.setDisplayLocation((getWidth() - width)/2,(getHeight() - height)/2);
control.setVisible(true);
status = s + ": DW=" + width + "-DH=" + height + "-SW=" + control.getSourceWidth() + "-SH=" + control.getSourceHeight();
}
repaint();
setTitle(status);
}
}
dcrocha 13:43, 31 October 2007 (EET)


hi I tried compiling this source but am getting some error.
is one of the line having error, and each line having ExampleMIDlet midlet contains and error.
Next error was at
how do i solve this error. both the error messages are same cannot find symbol.
it will be a gr8 help if you can help me out. I am trying to stream video from a web cam.
thanks in advance
Shuvo
=>
Here is an example :
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.List; import javax.microedition.lcdui.Display;
public class ExampleStreamingMIDlet extends MIDlet {
public List list;
public ExampleStreamingMIDlet() { list = new List("Accueil", List.IMPLICIT); }
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException { VideoCanvas VC = new VideoCanvas(this); Display.getDisplay(this).setCurrent(VC); }
}
RTSP link?
Is the RTSP link rtsp://rtsp.youtube.com/youtube/videos/HtVbAazUekM/video.3gp still working?
I get the exception:
javax.microedition.media.MediaException: Cannot create a Player for: rtsp://rtsp.youtube.com/youtube/videos/HtVbAazUekM/video.3gp at javax.microedition.media.Manager.createPlayer(+45)
Thanks,.
Christelle
Viraj.mehta - Prefetch error -2
Hello,
After implementation of this code, I get the alert 'Prefetch error -2'. The url is the live stream of an IP camera and it is in mp4 format h.264 baseline profile. Any ideas why this may be happening ?
Thanks,
Virajviraj.mehta 12:56, 4 April 2012 (EEST)
GTO India - Playing rtsp stream or any video sream on Nokia Asha mobiles
where is the completes code / project so that I can build one Live TV ap for BlackBerry device?
Iam unable to play the rtsp video on BB sulator too.
Please solve my query.
Thanks
shravan kumrGTO_India 12:09, 11 January 2013 (EET)