Question

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?

Was it helpful?

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

OTHER TIPS

The previous answer deals with vlcj-3.x. The API for full-screen has changed with vlcj-4.x and later.

For a similar implementation as the other answer, but with the new API for vlcj-4.x, here is an example:

// Standard vlcj code to create a UI and the media player...
Canvas c = new Canvas();
c.setBackground(Color.black);

JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);

JFrame f = new JFrame("VLCJ");
f.setContentPane(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);

mediaPlayerFactory = new MediaPlayerFactory();
mediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
mediaPlayer.videoSurface().set(mediaPlayerFactory.videoSurfaces().newVideoSurface(c));

// This enables native full-screen, it will pick the right implementation
// for the run-time OS - you could pick a specific implementation like
// Win32FullScreenStrategy here instead
mediaPlayer.fullScreen().strategy(new AdaptiveFullScreenStrategy(f));

f.setVisible(true);

// Enter full-screen mode
mediaPlayer.fullScreen().set(true);

mediaPlayer.media().play("some-movie.mp4");

Note that instead of setting this up yourself, like linking the video surface to the video player and so on, you are recommended to use the EmbeddedMediaPlayerComponent instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top