我使用vlcj创建多个视频面板。 对于每一个视频窗口,我要补充的代码:
factory0 = new MediaPlayerFactory();
mediaPlayer0 = factory0.newEmbeddedMediaPlayer();
mediaPlayer0.setVideoSurface(factory0.newVideoSurface(videoCanvas[0]));
mediaPlayer0.setPlaySubItems(true);
...
如果我想有8个视频面板,然后我将不得不重复这些代码(非动态)。
我的问题是:我想有MediaPlayerFactory和MediaPlayerFactory为数组,这样我可以缩短我的代码,然后这将通过仅改变Num_Video动态表现。
我完整的代码如下所示。 我在制作的mainPanel 4个视频面板。
谢谢。
import uk.co.caprica.vlcj.binding.internal.libvlc_media_t;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
public class MultiThreadedWindow extends JFrame implements ActionListener {
private JPanel topPanel = new JPanel();
private JPanel statusPanel = new JPanel();
private JButton playButton = new JButton("Play");private JButton btnStop = new JButton("Stop");
private JButton btnPause = new JButton("Pause");
private MediaPlayerFactory factory0, factory1, factory2, factory3;
private EmbeddedMediaPlayer mediaPlayer0, mediaPlayer1, mediaPlayer2, mediaPlayer3;
int Video_Width = 200;
int Video_Height = 150;
int Num_Video = 4;
public MultiThreadedWindow() {
final String jnaLibraryPath = System.getProperty("jna.library.path");
final StringBuilder newJnaLibraryPath = new StringBuilder(jnaLibraryPath != null ? (jnaLibraryPath + ":") : "");
newJnaLibraryPath.append("/Applications/VLC.app/Contents/MacOS/lib");
System.setProperty("jna.library.path", newJnaLibraryPath.toString());
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
btnStop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stop();
}
});
btnPause.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pause();
}
});
topPanel.add(playButton);
topPanel.add(btnStop);
topPanel.add(btnPause);
this.setSize(1000, 800);
this.setLayout(new BorderLayout());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new FlowLayout());
Canvas[] videoCanvas = new Canvas[Num_Video];
JPanel[] vidPanel = new JPanel[Num_Video];
for (int i = 0; i < Num_Video; i++) {
vidPanel[i] = new JPanel();
videoCanvas[i] = new Canvas();
vidPanel[i].setPreferredSize(new Dimension(Video_Width, Video_Height));
vidPanel[i].setBackground(Color.black);
vidPanel[i].setLayout(new BorderLayout());
videoCanvas[i].setBackground(Color.black);
vidPanel[i].add(videoCanvas[i], BorderLayout.CENTER);
mainPanel.add(vidPanel[i]);
}
factory0 = new MediaPlayerFactory();
mediaPlayer0 = factory0.newEmbeddedMediaPlayer();
mediaPlayer0.setVideoSurface(factory0.newVideoSurface(videoCanvas[0]));
mediaPlayer0.setPlaySubItems(true);
mediaPlayer0.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
List<String> items = mediaPlayer.subItems();
System.out.println(items);
}
});
factory1 = new MediaPlayerFactory();
mediaPlayer1 = factory1.newEmbeddedMediaPlayer();
mediaPlayer1.setVideoSurface(factory1.newVideoSurface(videoCanvas[1]));
mediaPlayer1.setPlaySubItems(true);
mediaPlayer1.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
List<String> items = mediaPlayer.subItems();
System.out.println(items);
}
});
factory2 = new MediaPlayerFactory();
mediaPlayer2 = factory2.newEmbeddedMediaPlayer();
mediaPlayer2.setVideoSurface(factory2.newVideoSurface(videoCanvas[2]));
mediaPlayer2.setPlaySubItems(true);
mediaPlayer2.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
List<String> items = mediaPlayer.subItems();
System.out.println(items);
}
});
factory3 = new MediaPlayerFactory();
mediaPlayer3 = factory3.newEmbeddedMediaPlayer();
mediaPlayer3.setVideoSurface(factory3.newVideoSurface(videoCanvas[3]));
mediaPlayer3.setPlaySubItems(true);
mediaPlayer3.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
List<String> items = mediaPlayer.subItems();
System.out.println(items);
}
});
this.add(topPanel, BorderLayout.NORTH);
this.add(mainPanel, BorderLayout.CENTER);
this.add(statusPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) throws IllegalStateException {
if (e.getActionCommand().equals("OK")) {
this.dispose();
}
}
private void start() {
this.setVisible(true);
}
private void play() {
mediaPlayer0.playMedia("/Users/pujan/1-pix.mp4");
mediaPlayer1.playMedia("/Users/pujan/1-shakira.mp4");
mediaPlayer2.playMedia("/Users/pujan/1-flowers.mp4");
mediaPlayer3.playMedia("/Users/pujan/1-aam.mp4");
}
private void stop() {
mediaPlayer0.stop();
mediaPlayer1.stop();
mediaPlayer2.stop();
mediaPlayer3.stop();
}
private void pause() {
mediaPlayer0.pause();
mediaPlayer1.pause();
mediaPlayer2.pause();
mediaPlayer3.pause();
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MultiThreadedWindow().start();
}
});
}
}