1

I am using VLCJ binding to create a video player.

My code:

public class MyVideoPlayer {

    private EmbeddedMediaPlayerComponent mediaPlayerComponent;
    static String VLCLIBPATH = "C:\\Program Files\\VideoLAN\\VLC";

    public MyVideoPlayer(String source) {
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), VLCLIBPATH);
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
        JFrame frame = new JFrame("VLC Player");
        mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setContentPane(mediaPlayerComponent);
        frame.setSize(1366, 768);
        frame.setVisible(true);
        mediaPlayerComponent.getMediaPlayer().playMedia(source);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    } 
}

I call this video player from another frame by new VideoPlayer(source). When I use JFrame.DISPOSE_ON_CLOSE, the frame gets closed but the sound still won't go..

How can I close the video player frame completely?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2
  1. Declare the close operation as JFrame.DO_NOTHING_ON_CLOSE.
  2. Add a WindowListener as follows (adapt to your code as needed):

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            mediaPlayerComponent.getMediaPlayer().stop(); // Very important!
            frame.dispose();
        }
    });
    

It will likely be necessary to declare frame & mediaPlayerComponent as final in order to access them from within an inner class.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Use JFrame.EXIT_ON_CLOSE

i.e frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

The exit application default window close operation. If a window has this set as the close operation and is closed in an applet, a SecurityException may be thrown. It is recommended you only use this in an application.

https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#EXIT_ON_CLOSE

If you don't want to use Exit on close which will close your application then

Add a Listener on windowClosing, when the listener call the method stop the audio

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println("Closed");
        //Here stop the audio
        e.getWindow().dispose();
    }
});
Ashraful Islam
  • 12,470
  • 3
  • 32
  • 53