3

我正在使用 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();
            }
        });
    }
}
4

2 回答 2

1

我可以通过这个来实现

EmbeddedMediaPlayer mediaPlayer[] = new EmbeddedMediaPlayer[Num_Video];

然后在循环中

factory[i] = new MediaPlayerFactory();
mediaPlayer[i] = factory[i].newEmbeddedMediaPlayer();
mediaPlayer[i].setVideoSurface(factory[i].newVideoSurface(videoCanvas[i]));
mediaPlayer[i].setPlaySubItems(true);
于 2012-06-19T17:12:22.797 回答
0

你可以做:

for (int i = 0; i < numVideo; i++) {
    vidPanel[i] = new JPanel();
    videoCanvas[i] = new Canvas();
    vidPanel[i].setPreferredSize(new Dimension(larguraVideo, alturaVideo));
    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]);

    factory[i] = new MediaPlayerFactory();
    mediaPlayer[i] = factory[i].newEmbeddedMediaPlayer();
    mediaPlayer[i].setVideoSurface(factory[i].newVideoSurface(videoCanvas[i]));
    mediaPlayer[i].setPlaySubItems(true);
    mediaPlayer[i].addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
        @Override
        public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem) {
            List<String> items = mediaPlayer.subItems();
            System.out.println(items);
        }
    });
}

告诉我:经过这么长时间,你的代码怎么样?

于 2018-09-25T14:39:48.807 回答