Search code examples
javajframejtextfieldvlcj

adding a JTextField on top of a video played by VLCJ?


i am trying to add a JTextField on top of a video when reaching a specific time. I'm using this code:

        frame.setLocation(100, 100);
        frame.setSize(0, 0);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setLayout(null);

        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);

        mediaURL = "D:\\Desktop\\movie.mp4";

        MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
        mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(frame));
        mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
        scoresFrame.setVisible(true);
        mediaPlayer.toggleFullScreen();
        mediaPlayer.setEnableMouseInputHandling(false);
        mediaPlayer.setEnableKeyInputHandling(true);
        mediaPlayer.prepareMedia(mediaURL);
        //mediaPlayer.setRepeat(true);
        mediaPlayer.play();

        mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            public void timeChanged (MediaPlayer mediaPlayer , long time) {
                if (time > 1000 && time < 1500) {
                    frame.setLayout(null);
                    mediaPlayer.pause();
                    JTextField text = new JTextField("test");
                    text.setBounds(250, 370, 50, 50);
                    frame.add(text);
                }
            }
        });

the JTextField does not show up at all. what can the problem be? and any other suggetions to pause the video at a specific time? because using this code:

public void timeChanged (MediaPlayer mediaPlayer , long time) {
                    if (time > 1000 && time < 1500) {
                    }
                }

doesn't seen to be the best way. Thank you for helping.


Solution

  • You can't mix lightweight components (your JTextField) and heavyweight components (the Canvas you use for vlcj).

    You have two options...

    1. Use a transparent overlay, adding your JTextField to the overlay (there are examples in the vlcj test sources that show this)
    2. Use a 'direct' media player, where you render the video yourself into a lightweight component, i.e. a JPanel or something (again, there are examples in the vlcj test sources that show this)