I have fully a functional VLCj-based video player as shown bellow.
Working code
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.discovery.NativeDiscovery;
/**
* Minimal quick-start example.
*/
public class Example1 {
private final JFrame frame;
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
public static void main(String[] args) {
new NativeDiscovery().discover();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Example1().start("file:///C:/video.avi");
}
});
}
public Example1() {
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
frame = new JFrame("vlcj quickstart");
frame.setLocation(50, 50);
frame.setSize(1400, 800);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(mediaPlayerComponent, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(320, 240));
frame.pack();
frame.setVisible(true);
}
private void start(String mrl) {
mediaPlayerComponent.getMediaPlayer().playMedia(mrl);
}
}
Modifications to the code in order to draw over the video
What I need is to draw over the video (for example a rectangle). For this purpose I have created MyJPanel.
class MyPanel extends JPanel {
private EmbeddedMediaPlayerComponent comp;
public MyPanel(EmbeddedMediaPlayerComponent mediaPlayerComponent) {
add(this.comp = mediaPlayerComponent);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawRect(10, 10, 200, 100);
}
}
And instead of the line:
frame.getContentPane().add(mediaPlayerComponent, BorderLayout.CENTER);
I added:
frame.getContentPane().add(new MyPanel(mediaPlayerComponent), BorderLayout.CENTER);
Problem:
After adding MyPanel I am getting this error: and no any video is being displayed.
[0000000029d930e0] avi demux error: no key frame set for track 0
[0000000029e035d0] core vout display error: Failed to set on top
You simply can not use Java2D to draw on top of the heavyweight AWT Canvas
video surface.
There are however a number of other approaches you can use to render on top of the video:
I think #4, whilst not ideal, is probably the best you can do and is closest to what you're asking for.