使用 VLCJ 将视频播放器添加到 Java 中的 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10777279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:31:48  来源:igfitidea点击:

Adding a video player to a JPanel in java using VLCJ

javaswingvideovlcj

提问by jazibobs

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.

我目前有两件我希望合并的工作。我有一个在 JFrame 中运行的简单媒体播放器和一个 GUI,我想在 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"

我收到错误消息:“vidPanel 无法解析或不是字段”

Does anyone know how I can rectify this?

有谁知道我该如何解决这个问题?

回答by pelutxe

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:

我遇到了同样的问题,今天才解决。问题是您使用的是 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);

I'm using JDK 1.6 and VLCJ 2.1

我使用的是 JDK 1.6 和 VLCJ 2.1

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

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

Good luck

祝你好运

回答by Michael Berry

Firstly, it looks like your vidPanelis 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.

首先,看起来你vidPanel是一个局部变量,如果你需要从其他方法访问它应该是一个字段。这是 Java 的一个非常基本的部分 - 任何初学者教程都应该涵盖这一点。VLCJ 不是最容易使用的东西,如果您不清楚基本原理,您可能会不知所措。

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.

其次,在您走得太远之前,嵌入式 VLCJ 播放器不能与 JPanel 一起使用,而只能与本机 AWT Canvas 一起使用——因此您需要改用它。

回答by Hakan Serce

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

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

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.

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

Then instead of the erroneous line use the following:

然后,而不是错误的行使用以下内容:

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