W
wish
Hi,
i'm trying to build a simple app using tutorial based player, that would
display a couple of videos at a time in a paused state showing the first
frame of each of them.
This gives me a headache, because calling start() on a player and stop()
directly after its realisation seems to make its resources impossible to
destroy. The effect is, that after refreshing the screen 3 times with 12
thumbnails on it my program is occupying over 200 MB of memory .
I've noticed that simple calling start(), playing and then destroying player
as it is said in all documentation works well. Perhaps there is some
timeline in which methods, garbage collector and other stuff should be
called in order to close everything properly? I've gone through every
possible newsgroup and can't find the answer.
Here is a code sample from my player class. I'd be grateful for any help on
this.
Thx.
public class VideoComponent extends JPanel implements ControllerListener {
[.........Cut..........]
// ------------------------------------------------------------------------
public void init() {
MediaLocator mrl;
URL url;
File file;
this.setOpaque(false);
this.setLayout(new BorderLayout());
this.setBackground(Color.black);
panel = new JPanel();
panel.setOpaque(false);
panel.setLayout(new BorderLayout());
this.add(panel, BorderLayout.CENTER);
try {
file = new File(videoPath);
url = new URL("file:///" + file.getAbsolutePath());
mrl = new MediaLocator(url);
// jesli nie ma pliku wywal blad
if (mrl == null) error("No media locator");
try {
player = Manager.createPlayer(mrl);
} catch (Exception e) {
e.printStackTrace();
error(e.getMessage());
}
/* dodaj ta klase jako listener playera */
player.addControllerListener(this);
} catch (MalformedURLException e) {
error("Invalid media file URL!");
} catch (IOException e) {
error("IO exception creating player");
}
start();
}
// ------------------------------------------------------------------------
public void start() {
if (player == null) return;
player.start();
playing = true;
}
// ------------------------------------------------------------------------
public void stop() {
if (player != null) {
player.stop();
player.deallocate();
}
playing = false;
}
// ------------------------------------------------------------------------
public void pause() {
if (player != null) player.stop();
playing = false;
}
// ------------------------------------------------------------------------
public void destroy() {
if (player != null) player.close();
playing = false;
}
// ------------------------------------------------------------------------
private void removeView() {
panel.removeAll();
controlComponent.dispose();
visualComponent = null;
controlComponent = null;
}
// ------------------------------------------------------------------------
private void removePlayer() {
player.removeControllerListener ( this );
player = null;
}
// ------------------------------------------------------------------------
public synchronized void controllerUpdate(ControllerEvent event) {
[.........Cut..........]
if (event instanceof RealizeCompleteEvent) {
if (!autoPlay) pause();
[.........Cut..........]
} else if (event instanceof EndOfMediaEvent) {
if (!loop) return;
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
stop();
destroy();
error(event.toString());
} else if (event instanceof ControllerClosedEvent) {
removeView();
removePlayer();
}
}
// ------------------------------------------------------------------------
private void error(String msg) {
System.out.println(msg);
}
}
i'm trying to build a simple app using tutorial based player, that would
display a couple of videos at a time in a paused state showing the first
frame of each of them.
This gives me a headache, because calling start() on a player and stop()
directly after its realisation seems to make its resources impossible to
destroy. The effect is, that after refreshing the screen 3 times with 12
thumbnails on it my program is occupying over 200 MB of memory .
I've noticed that simple calling start(), playing and then destroying player
as it is said in all documentation works well. Perhaps there is some
timeline in which methods, garbage collector and other stuff should be
called in order to close everything properly? I've gone through every
possible newsgroup and can't find the answer.
Here is a code sample from my player class. I'd be grateful for any help on
this.
Thx.
public class VideoComponent extends JPanel implements ControllerListener {
[.........Cut..........]
// ------------------------------------------------------------------------
public void init() {
MediaLocator mrl;
URL url;
File file;
this.setOpaque(false);
this.setLayout(new BorderLayout());
this.setBackground(Color.black);
panel = new JPanel();
panel.setOpaque(false);
panel.setLayout(new BorderLayout());
this.add(panel, BorderLayout.CENTER);
try {
file = new File(videoPath);
url = new URL("file:///" + file.getAbsolutePath());
mrl = new MediaLocator(url);
// jesli nie ma pliku wywal blad
if (mrl == null) error("No media locator");
try {
player = Manager.createPlayer(mrl);
} catch (Exception e) {
e.printStackTrace();
error(e.getMessage());
}
/* dodaj ta klase jako listener playera */
player.addControllerListener(this);
} catch (MalformedURLException e) {
error("Invalid media file URL!");
} catch (IOException e) {
error("IO exception creating player");
}
start();
}
// ------------------------------------------------------------------------
public void start() {
if (player == null) return;
player.start();
playing = true;
}
// ------------------------------------------------------------------------
public void stop() {
if (player != null) {
player.stop();
player.deallocate();
}
playing = false;
}
// ------------------------------------------------------------------------
public void pause() {
if (player != null) player.stop();
playing = false;
}
// ------------------------------------------------------------------------
public void destroy() {
if (player != null) player.close();
playing = false;
}
// ------------------------------------------------------------------------
private void removeView() {
panel.removeAll();
controlComponent.dispose();
visualComponent = null;
controlComponent = null;
}
// ------------------------------------------------------------------------
private void removePlayer() {
player.removeControllerListener ( this );
player = null;
}
// ------------------------------------------------------------------------
public synchronized void controllerUpdate(ControllerEvent event) {
[.........Cut..........]
if (event instanceof RealizeCompleteEvent) {
if (!autoPlay) pause();
[.........Cut..........]
} else if (event instanceof EndOfMediaEvent) {
if (!loop) return;
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
stop();
destroy();
error(event.toString());
} else if (event instanceof ControllerClosedEvent) {
removeView();
removePlayer();
}
}
// ------------------------------------------------------------------------
private void error(String msg) {
System.out.println(msg);
}
}