Adding a video player to a JPanel in java using VLCJ

跟風遠走 提交于 2019-12-24 17:46:02

问题


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?


回答1:


Firstly, it looks like your vidPanel is a local variable and should be a field if you need to access it from other methods. This is a pretty basic piece of Java - any beginners tutorial should cover this. VLCJ isn't the simplest thing to use and you may come unstuck if you're not clear on the fundamentals.

Secondly, before you head too far down that track, an embedded VLCJ player doesn't work with a JPanel, just a native AWT Canvas - so you'll need to use that instead.




回答2:


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




回答3:


First of all it seems to me that vidPanel is defined as a local variable, make it a member field by defining in the class scope (not in a method).

This is not how you do in a real maintainable code, but just to make a quick solution to your problem: Define a getVidPanel() function in MainWindow which returns vidPanel.

Then instead of the erroneous line use the following:

MainWindow aMainWindowInstance = new MainWindow();
aMainWindowInstance.getVidPanel().add(vidComp);


来源:https://stackoverflow.com/questions/10777279/adding-a-video-player-to-a-jpanel-in-java-using-vlcj

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!