Search code examples
javaswingvlclibvlcvlcj

Use cloned VLCJ in multiple windows


I want to have two copies of same video in two different JFrames! At the moment I have achieved that 2 videos is displaying but only one is "integrated" into a JFrame. As far as I've read it's not fully developed to be integrated with all copies.. Am I wrong in this? Also is there any other way to achieve what I want?

This is my code playing stuff in 2 JFrames and also a JFileChooser for simplicity.

public class MyMultiDisplay
{
    List <String> argsList;
    MediaPlayerFactory mediaPlayerFactory;
    EmbeddedMediaPlayer player;
    JFrame frame1;
    JFrame frame2;

    public static void main(String[] args)
    {
        new NativeDiscovery().discover();

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new MyStart();
            }
        });
    }

    public MyMultiDisplay()
    {
        frame1 = new JFrame("My First Media Player");
        frame1.setBounds(100, 100, 600, 400);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2 = new JFrame("My Second Media Player");
        frame2.setBounds(700, 500, 600, 400);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        argsList = new ArrayList <String>();
        argsList.add("--video-splitter=clone");
        argsList.add("--clone-count=2");
        mediaPlayerFactory = new MediaPlayerFactory(argsList);
        player = mediaPlayerFactory.newEmbeddedMediaPlayer();
        Canvas canvas = new Canvas();
        player.setVideoSurface(mediaPlayerFactory.newVideoSurface(canvas));
        frame1.add(canvas);
        frame1.setVisible(true);
        frame2.setVisible(true);
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            player.playMedia(chooser.getSelectedFile().getAbsolutePath());
        }
    }
}

Solution

  • Almost a year later I decided that for completeness i'll post my final solution here if anyone else ever find their way here!

    I created a library project using VLCJ as base able to in a really easy way use the DirektMediaPlayer to play the same video to multiple frames/windows/surfaces/JComponents/whatever in a really simple manner. It is not that resource heavy either in my unexperienced experience. 100 windows at the same time playing the same video and still no lag!

    Feel free to check it out!

    https://github.com/APayerl/VlcjWind