0

working with vlcj and java: making a instance from class B (the EmbeddedMediaPlayerComponent class) in class A:

mediaPlayerComponent = new EmbeddedMediaPlayerComponent();

opens via class B: new JFrame with a ebedded mediaPlayer.

I can destroy this 'JFrame' with:

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent windowEvent) {
    stillAlive = false;  
    release(true);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

public boolean stillAlive() {
    return stillAlive;
}

but mediaPlayerComponent in class A still exists and throws ugly errors if I try to reuse the mediaPlayerComponent

So if I want to play a new file, I do this. check if its the first time opening an instance and check if I did a release(true) via the stillAlive = false. Otherwise reuse the mediaPlayerComponent.

if (mediaPlayerComponent == null || (! mediaPlayerComponent.stillAlive())) { // keine facory
            mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
            mediaPlayerComponent.getMediaPlayer().prepareMedia(marked.getPath());
            mediaPlayerComponent.getMediaPlayer().play();

        } else {
            mediaPlayerComponent.getMediaPlayer().prepareMedia(marked.getPath());
            mediaPlayerComponent.getMediaPlayer().play();

        }

Is there a nicer way? Like telling class A to set mediaPlayerComponent = null. It somehow seams to be bad coding stile to carry this boolean around.

schasoli
  • 317
  • 1
  • 3
  • 12
  • Have you tried telling class A to set `mediaPlayerComponent = null`? – user253751 Feb 23 '15 at 23:35
  • class A does not know when JFrame is killed via `windowClosing`. I do not know how to call parent A from child B. `public class EmbeddedMediaPlayerComponent extends Panel implements MediaPlayerEventListener {` Maybe via interface: http://stackoverflow.com/questions/18279302/how-do-i-perform-a-java-callback-between-classes ? – schasoli Feb 24 '15 at 00:11
  • Your question is not that clear to me, perhaps a piece of code that shows everything together might be clearer. Anyway, in general with vlcj I recommend you should not release media players until the application terminates. Maybe it would also help to hide your frame and re-use it rather than dispose it. – caprica Feb 24 '15 at 07:31
  • hide and re-use sounds a lot better. If I use `System.exit(0);` at main, all players a closed, so no `release()` necessary. – schasoli Feb 24 '15 at 08:43
  • Using release() is prudent however, because you may leak native resources if you don't invoke release() on the media player. – caprica Feb 24 '15 at 09:59
  • Every mediaPlayerComponent has its one JFrame. Main does not know witch instance of mediaPlayerComponent are existing. Is there a way, to release() all? – schasoli Feb 24 '15 at 11:22

0 Answers0