0

I have an applet that runs VLCJ (http://code.google.com/p/vlcj/) - basically embedding a VLC player in an applet. When running in eclipse, it runs well but I cannot close the debugging applet-window or terminate it somehow. I wonder, why is this? Is there anything in the code that prevents it from stopping debugging? I have to restart eclipse in order to make it quit. Im quite sure you dont need to add destroy() to enable closing of the debugging window.

Thanks

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Frame;
import javax.swing.JApplet;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;


public class Main extends JApplet {

    /**
     * @param args
     */
    /* entry point */
    public void init() {
        String file = "110825-155446.wmv";    // only 2-3 seconds clip for minimum storage      
        runVideo(file);
    }

    /* runs the video file */
    public void runVideo(String file) {

        setSize(400,300);
        setLayout(new BorderLayout()); 
        Canvas vs = new Canvas();
        add(vs,BorderLayout.CENTER);
        setVisible(true);

        MediaPlayerFactory factory = new MediaPlayerFactory();

        EmbeddedMediaPlayer mediaPlayer = factory.newEmbeddedMediaPlayer();
        mediaPlayer.setVideoSurface(factory.newVideoSurface(vs));

        mediaPlayer.playMedia(file);
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }


}
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

1 Answers1

0

The reason is at this code snippet part:

try {
    Thread.currentThread().join();
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

It blocks the application from closing since it doesn't want to return to the system. Thread.join() makes the current thread waits for another thread to complete, basically it waits forever.

To improve it, you can do like this (as in http://code.google.com/p/vlcj/wiki/MinimalMp3Player):

mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
  public void finished(MediaPlayer mediaPlayer) {
    System.exit(0);
  }
  public void error(MediaPlayer mediaPlayer) {
    System.exit(1);
  }
});
mediaPlayer.playMedia(args[0]);
Thread.currentThread().join();

However, we cannot use System.exit() method in a Java servlet code (or even applet code) as it can shutdown the JVM used by the code which may be needed by other Java application/servlet/applet codes. See Alternatives to System.exit(1), Calling System.exit() in Servlet's destroy() method

Community
  • 1
  • 1
ee.
  • 947
  • 5
  • 5