Adding a video player to a JPanel in java using VLCJ

jazibobs picture jazibobs · May 27, 2012 · Viewed 10.6k times · Source

I am currently in the position of having 2 pieces of work I wish to combine. I have a simple media player running in a JFrame and a GUI I would like to add video playback to on a JPanel.

The code for the which creates video player window is as follows:

private final JFrame vidFrame;
private final EmbeddedMediaPlayerComponent vidComp;

//Creates JPanel for video player
public Video() {

    vidFrame = new JFrame("VLC video test");
    vidFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    vidFrame.setLocation(100, 100);
    vidFrame.setSize(800, 800);

    vidComp = new EmbeddedMediaPlayerComponent();

    //This is the point where I am trying to add the video player to the GUI
    MainWindow.vidPanel.add(vidComp);

    vidFrame.add(vidComp);
    vidFrame.setVisible(true);
}

And this is the panel I'm trying to add the player to:

    JPanel vidPanel = new JPanel();
    vidPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    vidPanel.setBounds(10, 11, 532, 400);
    contentPane.add(vidPanel);

I get the error message: "vidPanel cannot be resolved or is not a field"

Does anyone know how I can rectify this?

Answer

pelutxe picture pelutxe · May 30, 2012

I've had the same problem and just solve it today. The problem is you're using a JPanel and you'll never be able to watch a video there, you should use a Canvas instead. This is what worked for me:

    Canvas canvas = new Canvas();
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
    mediaPlayer.setVideoSurface(videoSurface);

    mediaPlayer.playMedia(String with the name of the file);

I'm using JDK 1.6 and VLCJ 2.1

If you're using an IDE just place a Canvas exactly as you placed the JPanel and delete the first line.

Good luck