本文介绍了VLCJ创建多个视频面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vlcj创建多个视频面板。对于每个视频窗口,我应该添加代码:

I am creating multiple video panels using vlcj. For each video window I should add code:

 factory0 = new MediaPlayerFactory();
 mediaPlayer0 = factory0.newEmbeddedMediaPlayer();
 mediaPlayer0.setVideoSurface(factory0.newVideoSurface(videoCanvas[0]));
 mediaPlayer0.setPlaySubItems(true);
 ...

如果我想要8个视频面板,那么我将不得不重复这些代码(不是动态的)。

If I want to have 8 video panels then I will have to repeat these codes (not dynamic).

我的问题是:我想将MediaPlayerFactory和MediaPlayerFactory作为数组,以便我可以缩短我的代码,然后这将动态行为只需更改Num_Video。

My Question is: I'd like to have MediaPlayerFactory and MediaPlayerFactory as arrays so that I can shorten my code and then this will behave dynamically by just changing Num_Video.

我的完整代码如下。我在mainPanel中创建了4个视频面板。

My complete code is as follows. I am creating 4 videos panels in mainPanel.

谢谢。

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


推荐答案

我能够通过这个实现

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

这篇关于VLCJ创建多个视频面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:40