如何在JPanel上播放mp4视频?

时间:2013-11-27 14:47:03

标签: java swing video mp4 xuggle

我正在使用Xuggle库在JPanel上播放mp4视频,但视频加载需要3秒。或者更多。你有什么建议如何以正确的方式在JPanel或JLabel上播放视频?

这是展示mp4视频的好方法吗? VideoCodec是一个Xuggle Codec。这是有效的,但我有几秒的延迟。

public void setVideoName(final String videoName) {
    imageAndVideoPanel.removeAll();
    final VideoPanel videoPanel = new VideoPanel();
    videoPanel.setPreferredSize(Const.Dimensions.VIDEO_SIZE);
    videoPanel.setMinimumSize(Const.Dimensions.VIDEO_SIZE);
    videoPanel.setMaximumSize(Const.Dimensions.VIDEO_SIZE);
    imageAndVideoPanel.add(videoPanel);

    new Thread(new Runnable() {
        @Override
        public void run() {
            VideoCodec videoCodec =
                    new VideoCodec(videoPanel, videoName + TextsDao.getText("videoFilesExtension"));
        }
    }).start();
}

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。 VLCJ库和EmbeddedMediaPlayer。播放视频/图像的代码很简单:

public class ExamQuestionsLeftPanel extends JPanel {
private EmbeddedMediaPlayerComponent component;
private EmbeddedMediaPlayer player;

...

public ExamQuestionsLeftPanel() {
    setUpPanel();
    initializeComponents();
}

private void setUpPanel() {
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "VLCx86");
    component = new EmbeddedMediaPlayerComponent();
    player = component.getMediaPlayer();

    Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);

    setLayout(null);
    setBackground(Const.Colors.EXAM_BACKGROUND_COLOR);
    setAlignmentX(Component.LEFT_ALIGNMENT);
    setBorder(emptyBorder);
}

...

public void setImageName(String imageName) {
    player.stop();
    player.prepareMedia("media" + File.separator + imageName);
    player.parseMedia();
    player.play();
}

public void setVideoName(final String videoTitle) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            player.stop();
            player.prepareMedia("media" + File.separator + videoTitle);
            player.parseMedia();
            player.play();
        }
    }).start();
}