从一系列vlcj播放器播放视频

时间:2013-12-24 12:23:35

标签: java swing vlc libvlc vlcj

我正在尝试播放以字符串形式提供的MRL列表中的视频。

问题是当我尝试运行该类时,一个面板列表显示按钮,只有一个面板工作,但播放按钮不起作用和其他面板的工作。

虽然我故意将停止按钮退出,因为我没有向他们添加动作侦听器。

我想要实现的是,当我上课时,播放一个视频,当我点击另一个视频的播放按钮时,当前视频停止并移动到下一个视频。

我不知道我哪里出错了。

这是我的代码:

public class MediaPlayer extends JPanel {

    //Declares our media player component
    private EmbeddedMediaPlayerComponent[] mediaplayer;
    private String[] mediapath = {""};
    private final String vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
    private JPanel video_pnl, control_pnl;
    private JButton[] play_btn, stop_btn;
    private int but = 0;

    public MediaPlayer(String mediapath[]) {
        this.mediapath = mediapath;

        play_btn = new JButton[1];
        stop_btn = new JButton[1];
        mediaplayer = new EmbeddedMediaPlayerComponent[1];
        int increment = 0;
        while (increment < mediapath.length) {
            video_pnl = new JPanel();
            video_pnl.setLayout(new BorderLayout());
            control_pnl = new JPanel();
            control_pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
            for (int i = 0; i < 1; i++) {

                NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcpath);
                mediaplayer[i] = new EmbeddedMediaPlayerComponent();
                play_btn[i] = new JButton("play");
                stop_btn[i] = new JButton("stop");


                video_pnl.add(mediaplayer[i], BorderLayout.CENTER);

                control_pnl.add(play_btn[i]);
                control_pnl.add(stop_btn[i]);
                video_pnl.add(control_pnl, BorderLayout.SOUTH);

                Handler handler = new Handler();
                play_btn[i].addActionListener(handler);
            }
            add(video_pnl);
            increment++;
        }
    }

    private class Handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == play_btn){
                play();
            }
        }
    }

    public void play() {
        for (int i = 0; i < mediapath.length; i++) {
            mediaplayer[i].getMediaPlayer().playMedia(mediapath[i]);
        }
    }

    public static void main(String[] args) {
        //Declare and initialize local variables
        String[] mediaPath =       {"C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Whistle.mp4", "C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Beyonce_Hello.mp4",
        "C:\\Users\\goldAnthony\\Desktop\\Videos\\HansRosling_2012S_480p.mp4","C:\\Users\\goldAnthony\\Desktop\\Videos\\oow2010_2.mp4",
        "C:\\Users\\goldAnthony\\Desktop\\Videos\\The_Economic_Environment.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.setLayout(new GridLayout(5, 1));
        ourframe.setSize(300, 560);
        ourframe.setVisible(true);
        mediaplayer.play();
        ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
                control_pnl.add(play_btn[i]);
                control_pnl.add(stop_btn[i]);
                video_pnl.add(control_pnl, BorderLayout.SOUTH);

                Handler handler = new Handler();
                play_btn[i].addActionListener(handler);
            }
            add(video_pnl);
            increment++;
        }
    }

    private class Handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == play_btn){
                play();
            }
        }
    }

    public void play() {
        for (int i = 0; i < mediapath.length; i++) {
            mediaplayer[i].getMediaPlayer().playMedia(mediapath[i]);
        }
    }

    public static void main(String[] args) {
        //Declare and initialize local variables
        String[] mediaPath =       {"C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Whistle.mp4", "C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Beyonce_Hello.mp4",
        "C:\\Users\\goldAnthony\\Desktop\\Videos\\HansRosling_2012S_480p.mp4","C:\\Users\\goldAnthony\\Desktop\\Videos\\oow2010_2.mp4",
        "C:\\Users\\goldAnthony\\Desktop\\Videos\\The_Economic_Environment.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.setLayout(new GridLayout(5, 1));
        ourframe.setSize(300, 560);
        ourframe.setVisible(true);
        mediaplayer.play();
        ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2 个答案:

答案 0 :(得分:2)

BorderLayout.CENTER的{​​{1}}只能包含一个组件。构造函数循环结束后,它引用最后添加的JPanel video_pnl。在您的监听器中,您可以使用CardLayout更改面板或更新单个面板。

答案 1 :(得分:0)

你的事件处理程序有这个:

if(e.getSource() == play_btn){

e.getSource()将返回点击的按钮。但是,play_btn是一个数组而不是一个按钮。因此,您要比较数组实例和按钮实例是否相等,并且始终为false。

实现目标的一种方法是使用动作命令:

play_btn[i] = new JButton("play");
play_btn[i].setActionCommand("play");

然后,您可以将事件处理程序中的测试更改为:

if(e.getActionCommand().equals("play") {

顺便说一句,这个问题实际上与vlcj没什么关系。