| Reply | « Previous Thread | Next Thread » |
|
Hi all,
I have an application which get from internet some video clips and I want to play them in my application. The approach is the next: Because the inputstream received is big i use 2 players for playing. So I get a part from inputstream and after that I start the paying. Immediately I get the next part for another player and after first finishes I start the second player and so on. My problem is that when I call player.realize() it takes very much( about 8 sec for a 5 sec clip) and in this time the application doesn't respond for any key. I mention that the buffering I make in a separated thread and I'm not understanding why the main thread is blocked while realize() is doing. So there is a way to keep the main thread to respond or there is an another way to reduce the realize() time? |
| cristi.mota |
| View Public Profile |
| Find all posts by cristi.mota |
|
How big is the 5 sec clip in size. Post the code if you can.
|
|
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;
}
}
}
|
| cristi.mota |
| View Public Profile |
| Find all posts by cristi.mota |
|
May you supply also IntegerConvertor.BigEndianQword2long to test your code? May you post this class?
|
| StefanMayr |
| View Public Profile |
| Find all posts by StefanMayr |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Prefecting a player takes a long time. Why is that? | amila325 | Mobile Java Media (Graphics & Sounds) | 1 | 2008-09-03 08:26 |
| Create 3GP player with J2ME for Mobile | dexxa05 | Mobile Java Media (Graphics & Sounds) | 53 | 2008-03-27 20:52 |
| 关于Float的问题,急死了,各位帮忙一下,谢谢! | show_up | Other Programming Discussion 关于其他编程技术的讨论 | 4 | 2004-08-17 09:57 |
| PC Connectity with VB6 | mkintanar | PC Suite API and PC Connectivity SDK | 5 | 2003-09-24 05:18 |