我正在尝试在JPanel中使用vlcj播放视频,但它对我不起作用。我收到的消息异常是:
“java.lang.IllegalStateException:视频表面组件必须可显示”
代码:
public class MediaPlayerPanel extends JPanel {
private static final long serialVersionUID = 1L;
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
Canvas c = new Canvas();
JPanel p = new JPanel();
public MediaPlayerPanel() {
c.setBackground(Color.black);
p.setLayout(new BorderLayout());
p.add(c, BorderLayout.CENTER);
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
c.setVisible(true);
p.setVisible(true);
}
public void play(String video) {
mediaPlayer.playMedia(video);
}
}
public class VideoPlayer {
public static void main(final String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
JFrame frame = new JFrame("Video Player");
MediaPlayerPanel mpp = new MediaPlayerPanel();
frame.setLocation(100, 100);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mpp, BorderLayout.CENTER);
frame.setVisible(true);
mpp.play("E:\\Filmek\\Game.of.Thrones.S04E02.HDTV.x264-2HD\\game.of.thrones.s04e02.hdtv.x264-2hd.mp4");
}
}
答案 0 :(得分:0)
该错误消息通常意味着您在展示(或mediaPlayer.play()
&#39}包含视频表面的应用程序框架之前尝试pack()
。
在您发布的代码中,您没有将面板p
添加到您创建的MediaPlayerPanel
面板中。因此,您的视频表面Canvas
实际上并未附加到任何可见的组件层次结构。
您的MediaPlayerPanel
应自行设置BorderLayout
,并为其添加p
。