1

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?

4

3 回答 3

3

我也遇到了同样的问题,今天才解决。问题是您使用的是 JPanel,您将永远无法在那里观看视频,您应该改用 Canvas。这对我有用:

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

我正在使用 JDK 1.6 和 VLCJ 2.1

如果您使用的是 IDE,只需按照您放置 JPanel 的方式放置 Canvas 并删除第一行。

祝你好运

于 2012-05-30T01:59:19.403 回答
1

首先,看起来您vidPanel是一个局部变量,如果您需要从其他方法访问它,它应该是一个字段。这是 Java 的一个非常基本的部分——任何初学者教程都应该涵盖这一点。VLCJ 不是最简单的使用方法,如果您不清楚基本原理,您可能会陷入困境。

其次,在你走得太远之前,嵌入式 VLCJ 播放器不能与 JPanel 一起使用,只能使用原生 AWT Canvas - 所以你需要使用它。

于 2012-05-28T16:14:47.853 回答
0

首先,在我看来,它vidPanel被定义为局部变量,通过在类范围内(而不是在方法中)定义使其成为成员字段。

这不是您在真正可维护的代码中所做的事情,而只是为了快速解决您的问题:getVidPanel()在 MainWindow 中定义一个返回vidPanel.

然后代替错误的行使用以下内容:

MainWindow aMainWindowInstance = new MainWindow();
aMainWindowInstance.getVidPanel().add(vidComp);
于 2012-05-27T21:58:55.163 回答