I'm trying to play a video using vlcj inside a JPanel but it doesn't work for me. The message exception I am getting is "java.lang.IllegalStateException: The video surface component must be displayable" which is the same problem as in Keep getting an Error "Component must be displayable".
The code of the JPanel which contains the canvas and the vlcj player is this:
import javax.swing.JPanel;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.Canvas;
import java.awt.Color;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.runtime.windows.WindowsCanvas;
public class MyJPanel extends JPanel {
private EmbeddedMediaPlayer player;
private WindowsCanvas canvas;
public MyJPanel() {
canvas = new WindowsCanvas();
add(canvas);
revalidate();
repaint();
canvas.setVisible(true);
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
player = mediaPlayerFactory.newEmbeddedMediaPlayer();
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
player.setVideoSurface(videoSurface);
player.playMedia("v.avi"); // This sentence throws the exception.
}
}
MyJFrame extends JFrame and only contains the MyJPanel JPanel. I think it's not important at all.
import javax.swing.JFrame;
public class MyJFrame extends JFrame {
protected MyJPanel myJPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyJFrame frame = new MyJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyJFrame() {
myJPanel = new myJPanel();
add(myJPanel);
}
}
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…