java - vlcj视频不显示,但可以听到视频声音

标签 java swing vlc libvlc vlcj

当应用程序运行时,会听到声音并且框架上可以看到其他按钮控件,但包含视频的面板不会显示任何黑屏,框架上也不会显示任何黑屏。

public class MediaPlayer extends JPanel {

private JFrame ourframe = new JFrame();
//Declares our media player component
private EmbeddedMediaPlayerComponent mediaplayer;
//This string holds the media URL path
private String mediapath = "";
//This string holds the vlc URL path
private final String vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
private JPanel panel, panel2;
private JButton play_btn, stop_btn, foward_btn, rewind_btn, enlarge_btn;
private JSlider timeline;

public MediaPlayer(String mediapath) {
    this.mediapath = mediapath;
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcpath);
    mediaplayer = new EmbeddedMediaPlayerComponent();

    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(mediaplayer, BorderLayout.CENTER); //panel containing the video

    play_btn = new JButton("play");
    stop_btn = new JButton("stop");
    foward_btn = new JButton("ff");
    rewind_btn = new JButton("rew");
    enlarge_btn = new JButton("enlarge");

    timeline = new JSlider(0, 100, 0);
    timeline.setMajorTickSpacing(10);
    timeline.setMajorTickSpacing(5);
    timeline.setPaintTicks(true);

    panel2 = new JPanel();
    panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel2.add(play_btn);
    panel2.add(stop_btn);
    panel2.add(foward_btn);
    panel2.add(rewind_btn);
    panel2.add(enlarge_btn);
    panel2.add(timeline);
    panel.add(panel2, BorderLayout.SOUTH);
    add(panel);
}

public void play() {
    mediaplayer.getMediaPlayer().playMedia(mediapath);
}

public static void main(String[] args) {
    //Declare and initialize local variables
    String mediaPath = "";
    mediaPath = "C:\\Users\\goldAnthony\\Desktop\\Videos\\Whistle.mp4";

    //creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run"
    MediaPlayer mediaplayer = new MediaPlayer(mediaPath);
    JFrame ourframe = new JFrame();
    ourframe.setContentPane(mediaplayer);
    ourframe.setSize(720, 560);
    ourframe.setVisible(true);
    mediaplayer.play();
    ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

最佳答案

这是因为您将视频表面添加到 JPanel 时没有选择适当的布局管理器。

默认情况下,JPanel 有一个 FlowLayout,这将根据组件的首选大小来布局组件。

视频表面面板没有设置首选尺寸。

解决办法是在主面板上设置一个像BorderLayout这样的布局管理器。

所以在你的构造函数的末尾,而不是这个......

add(panel);

...执行此操作...

setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);

交叉发布于 [1]。

[1] https://groups.google.com/forum/#!topic/vlcj-users/m0zG-fx4qm8

关于java - vlcj视频不显示,但可以听到视频声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728204/

相关文章:

java - 删除方法,从对象数组中删除对象

Java 7 String - 子串复杂度

java - 将 JButton 文本绑定(bind)到属性

javascript - VLC ActivX 插件无法在更新 IE9 中播放视频

android - VLC在此媒体Android上遇到错误

java - 使用通配符递归编译 .jar

java - GWT 应用程序/eclipse 插件使 JVM 崩溃

java - 如果我要包含图形用户界面,我是否需要重建我的应用程序?

java - 使用 JButton 填充 JList 并使用 Jlist 上的 DoubleClick 填充 JTextField

ubuntu - 如何在局域网中使用 VLC 从 BeagleBone Black 板流式传输视频?