It has 708KB.Here are the 3 classes used for this. I hope to understand. For starting playing you need an instance of StreamingComponent and than call play() method.
Code:
package streaming;
import java.io.IOException;
import java.io.InputStream;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class VideoInputStreamParser extends InputStream
{
InputStream stream = null;
long thisPieceLength = -1;
byte [] temp = new byte[128];
int readSoFar = 0;
public VideoInputStreamParser(InputStream in)
{
stream = in;
}
public int nextPiece()
{
// Read the first piece
if (thisPieceLength==-1)
{
try
{
// Read GUID (ignore it for now)
if ( stream.read(temp, 0, 38) == - 1 )
{
return -1;
}
// Read length
if ( stream.read(temp,0,8) == -1 )
{
return -1;
}
// Convert length
thisPieceLength = IntegerConvertor.BigEndianQword2long(temp);
// Reset readSoFar
readSoFar = 0;
} catch (IOException ex) {
return -1;
}
}
else
{
try
{
/* // Read until the next piece
int skipped = 0;
do
{
skipped += stream.skip(thisPieceLength - skipped);
} while (skipped < thisPieceLength); */
// Read GUID (ignore it for now)
if ( stream.read(temp, 0, 38) == - 1 )
{
return -1;
}
// Read length
if ( stream.read(temp,0,8) == -1 )
{
return -1;
}
// Convert length
thisPieceLength = IntegerConvertor.BigEndianQword2long(temp);
// Reset readSoFar
readSoFar = 0;
}
catch (IOException ex)
{
return -1;
}
}
return 1;
}
public final int read() throws IOException
{
if (readSoFar>=thisPieceLength)
{
return -1;
}
else
{
readSoFar++;
return stream.read();
}
}
}
Code:
package streaming;
import javax.microedition.lcdui.Canvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.media.control.VideoControl;
public class MTPlayer implements PlayerListener
{
private VideoInputStreamParser parser = null;
private Canvas canvas = null;
private Player player = null;
private VideoControl vc = null;
private boolean isFirst = false;
private String format;
private int top,left,width,height;
private Thread t;
/**
* Creates a new instance of a MultiThreading Player
*
* @param File to play
* @param Canvas to draw
*/
public MTPlayer( VideoInputStreamParser parser, Canvas canvas, int top, int left, int width, int height,String format)
{
this.canvas = canvas;
this.parser = parser;
this.top = top;
this.left = left;
this.width = width;
this.height = height;
this.format = format;
}
/**
* Wait until the current clip can play. If this is the first clip, no point in waiting.
*/
public void waitYourTurn()
{
if ( isFirst )
{
return;
}
synchronized ( this )
{
try
{
this.wait();
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
/**
* Attemps to play the clip.
*
*/
public synchronized void play()
{
try
{
player.start();
}
catch (MediaException ex)
{
ex.printStackTrace();
}
}
/**
* Call this if the current player is the first player in the "queue"
*/
public void makeFirst()
{
isFirst = true;
}
/**
* Link the current player to the next
* @param Next player
*/
public void link( MTPlayer next)
{
player.addPlayerListener(next);
}
public synchronized void playerUpdate(Player previousPlayer, String whatEvent, Object args)
{
if ( whatEvent.equals(PlayerListener.END_OF_MEDIA) )
{
previousPlayer.deallocate();
// When the previous player finishes, notify the current player that
// his turn has come ( see WaitYourTurn() )
synchronized (this)
{
this.notify();
}
play();
}
}
public boolean isFirtsPlayer() {
return isFirst;
}
void destroy() {
}
public synchronized void finalizeIt() {
try {
player = Manager.createPlayer(parser, format);
player.realize();
vc = (VideoControl) player.getControl("VideoControl");
vc.initDisplayMode(vc.USE_DIRECT_VIDEO, canvas);
vc.setDisplayLocation(left, top);
vc.setDisplaySize(this.width, this.height);
vc.setVisible(true);
} catch (Exception me) {
Output.echo("ERROR ME");
me.printStackTrace();
}
}
}
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package streaming;
import java.io.InputStream;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.game.GameCanvas;
import org.kalmeo.kuix.core.KuixMIDlet;
import org.kalmeo.util.worker.Worker;
/**
*
* @author Ovidiu
*/
public class StreamingComponent implements Runnable
{
public int top = 0, left = 0, height = 160, width = 120;
public String format = "video/mpeg";
public InputStream in = null;
private VideoInputStreamParser visp = null;
public static Thread streamThread;
MTPlayer cur, next;
public StreamingComponent()
{
streamThread = new Thread(this);
}
public void preloadFirstPiece() {
visp = new VideoInputStreamParser(in);
visp.nextPiece();
Output.echo("create first player");
cur = new MTPlayer(visp, KuixMIDlet.getDefault().getCanvas(), top, left, height, width, format);
}
public void play() {
cur.makeFirst();
cur.finalizeIt();
cur.play();
streamThread.start();
}
public void stopPlaying() {
}
public void run() {
while ((visp.nextPiece()) != -1) {
// Wait until previous player finishes.
// When previous player finishes, it should also auto-trigger the play
// of the current player, so no need to worry about that.
// While current players err.. plays, create a new player (another thread)
next = new MTPlayer(visp, KuixMIDlet.getDefault().getCanvas(), top, left, height, width, format);
synchronized(Worker.instance) {
next.finalizeIt();
}
// Link the current player to the next one
cur.link(next);
// The next player becomes the current one (a new cycle begins)
cur = next;
}
}
}