Playing a video from an array of vlcj player

不打扰是莪最后的温柔 提交于 2019-12-20 05:53:48

问题


I am trying to play a video from the lists of MRL provided as a String.

The problem is when I try to run the class, a list of panels shows with button, with only one panel working, but the play button does not work and that of other panels.

Although I intentionally left the stop button out because I have not added action listeners to them.

What I want to achieve is, when I run the class, a single video plays, and when I click on the play button of another video, the current video stops and moves to the next video.

I don't know where I have gone wrong.

Here is my code:

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);
    }
}

回答1:


The BorderLayout.CENTER of your JPanel video_pnl can only hold a single component. After your constructor's loop concludes, it references the last mediaplayer[i] added. In your listener, you can use CardLayout to change panels or update a single panel.




回答2:


Your event handler has this:

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

e.getSource() will return the button that was clicked. However, play_btn is an array not a button. You are therefore comparing an array instance and a button instance for equality and that will always be false.

One way to achieve what you want is to use action commands:

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

You can then change the test in your event handler to this:

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

By the way, this problem is really nothing at all to do with vlcj.



来源:https://stackoverflow.com/questions/20760900/playing-a-video-from-an-array-of-vlcj-player

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!