Search code examples
javaswingawtvlcvlcj

vlcj Full Screen video player


I have done a video player using vlcj library of vlc media player.

Here is my code to do that..

public class Player {

    public static void main(final String[] args) {
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Player(args);
            }
        });
    }

    private Player(String[] args) {
        JFrame frame = new JFrame("vlcj Tutorial");

        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();

        Canvas c = new Canvas();
        c.setBackground(Color.black);
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(c, BorderLayout.CENTER);
        frame.add(p, BorderLayout.CENTER);


        EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
        mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
        frame.setLocation(100, 100);
        frame.setSize(1050, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        mediaPlayer.playMedia("D:\\EmbeddedMediaPlayer\\test.3gp");
    }
}

I can play a video using this code, but how can I see the same video on full screen like vlc media player?

How to apply maximizing (full screen) and minimizing on a double-click event by mouse right button?


Solution

  • Full-screen can be somewhat problematic on different platforms, so the implementation of full-screen is left to a strategy implementation that you can choose or implement yourself.

    As it happens, Windows is the most problematic platform for full-screen.

    With vlcj 3.0.0+ there is a new full-screen strategy implementation that uses the Win32 native API. This is the most reliable and therefore recommended way to achieve full-screen on Windows.

    You choose the strategy implementation when you create your media player:

    EmbeddedMediaPlayer mediaPlayer = 
        mediaPlayerFactory.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
    

    Later when you want to toggle full-screen:

    mediaPlayer.toggleFullScreen();
    

    Or:

    mediaPlayer.setFullScreen(boolean fullScreen);
    

    If you want to listen to mouse-clicks, then in principle all you do is add a MouseListener as you would usually do to your Canvas object.

    However, with Windows it's not so simple to detect mouse-clicks, you must do this when you create your media player:

    mediaPlayer.setEnableMouseInputHandling(false);
    mediaPlayer.setEnableKeyInputHandling(false);
    

    Then in your MouseListener implementation you can invoke one of the full-screen methods described above.

    Note also that you must prevent your mediaPlayer reference from being garbage collected. Usually it is enough to declare it as a class field rather than a heap variable (as you have in your posted code).

    You should also look here for examples:

    https://github.com/caprica/vlcj/blob/vlcj-3.0.1/src/test/java/uk/co/caprica/vlcj/test/fullscreen

    https://github.com/caprica/vlcj/blob/vlcj-3.0.1/src/test/java/uk/co/caprica/vlcj/test/fullscreen/Win32FullScreenPlayerTest.java