我正在尝试播放以字符串形式提供的MRL列表中的视频。
问题是当我运行课程时,一个面板列表显示带有播放按钮和黑屏,但只有最后一个面板播放一个视频,视频列表中,以及当其他面板的播放按钮是点击,最后一个视频再次重播。
我想要实现的是,当我上课时,播放一个视频,当我点击另一个视频的播放按钮时,当前视频停止并移动到下一个视频。
我不知道我哪里出错了。
至于vlcj,我试过用户其他布局,但视频仅在使用BorderLayout时显示,这限制了我的选项
这是我的代码:
public class MediaPlayer2 extends JPanel {
//Declares our media player component
private EmbeddedMediaPlayerComponent mediaplayer;
private final String vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
private JPanel video_pnl, control_pnl;
private JButton play_btn;
ArrayList<String> file_location;
public MediaPlayer2(ArrayList<String> file_location) {
this.file_location = file_location;
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcpath);
int increment = 0;
while (increment < file_location.size()) {
for (int i = 0; i < 1; i++) {
video_pnl = new JPanel();
video_pnl.setLayout(new BorderLayout());
play_btn = new JButton("Play");
mediaplayer = new EmbeddedMediaPlayerComponent();
control_pnl = new JPanel();
control_pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
play_btn.setActionCommand("play");
video_pnl.add(mediaplayer, BorderLayout.CENTER);
control_pnl.add(play_btn);
video_pnl.add(control_pnl, BorderLayout.SOUTH);
Handler handler = new Handler();
play_btn.addActionListener(handler);
}
add(video_pnl);
increment++;
}
}
private class Handler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("play")) {
play();
}
}
}
public void play() {
for (int i = 0; i < file_location.size(); i++) {
mediaplayer.getMediaPlayer().playMedia(file_location.get(i));
}
}
public static void main(String[] args) {
//Declare and initialize local variables
ArrayList<String> file_location = new ArrayList<>();
file_location.add("C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Whistle.mp4");
file_location.add("C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Beyonce_Hello.mp4");
file_location.add("C:\\Users\\goldAnthony\\Desktop\\Videos\\HansRosling_2012S_480p.mp4");
file_location.add("C:\\Users\\goldAnthony\\Desktop\\Videos\\oow2010_2.mp4");
file_location.add("C:\\Users\\goldAnthony\\Desktop\\Videos\\The_Economic_Environment.mp4");
//creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run"
MediaPlayer2 mediaplayer = new MediaPlayer2(file_location);
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);
}
}