VLCJ上的多个流

时间:2018-09-11 19:16:17

标签: java vlcj

所以,我已经有了这段代码

public class MultiThreadedWindow extends JFrame implements ActionListener {

    FormatRtsp parser = new FormatRtsp();
    private JPanel topPanel = new JPanel();
    private JPanel statusPanel = new JPanel();
    private JButton btnPlay = new JButton("Conectar");
    private JButton btnStop = new JButton("Desconectar");
    private JButton btnPause = new JButton("Pausar");
    private MediaPlayerFactory[] factory;
    private EmbeddedMediaPlayer[] mediaPlayer;

    int Video_Width = 500;
    int Video_Height = 300;
    int Num_Video = 4;

    public MultiThreadedWindow() {
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setLayout(new BorderLayout());
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Canvas[] videoCanvas = new Canvas[Num_Video];
        JPanel mainPanel = new JPanel(new FlowLayout());
        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]);
        }
        for (int i = 0; i < 4; 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);
            }
        });
        }

        this.add(topPanel, BorderLayout.NORTH);
        this.add(mainPanel, BorderLayout.CENTER);
        this.add(statusPanel, BorderLayout.SOUTH);


        btnPlay.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(btnPlay);
        topPanel.add(btnStop);
        topPanel.add(btnPause);
    }

    public void start() {
        this.setVisible(true);
        play();
    }

    private void play() {
        mediaPlayer[0].playMedia(parser.formatRtspStream(RTSPADRESS));
        mediaPlayer[1].playMedia(parser.formatRtspStream(RTSPADRESS));
        mediaPlayer[2].playMedia(parser.formatRtspStream(RTSPADRESS));
        mediaPlayer[3].playMedia(parser.formatRtspStream(RTSPADRESS));
    }

    private void stop() {
        mediaPlayer[0].stop();
        mediaPlayer[1].stop();
        mediaPlayer[2].stop();
        mediaPlayer[3].stop();
    }

    private void pause() {
        mediaPlayer[0].pause();
        mediaPlayer[1].pause();
        mediaPlayer[2].pause();
        mediaPlayer[3].pause();
    }

    public void actionPerformed(ActionEvent e) throws IllegalStateException {
        if (e.getActionCommand().equals("OK")) {
            this.dispose();
        }
    }

}

这是一个使用VLCj显示4个摄像机的项目。我试图研究文档,甚至在Stackoverflow上的多个流中看到了该文档,但发现它令人困惑。我做了很多研究,但无法确定nullpointerexception在哪里。

它应该显示一个带有4个Canvas的JFrame。我能够执行相同的代码来声明4个工厂和4个媒体播放器,但是我认为这不是正确的方法。我想了解我所缺少的。谢谢。

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at core.MultiThreadedWindow.<init>(MultiThreadedWindow.java:56)

1 个答案:

答案 0 :(得分:0)

您在第56行看到了NPE,因为尚未在第10行(对于此情况,第9行)初始化了阵列。

其他代码使用类成员而不是数组来保存所有成员。

private MediaPlayerFactory factory0, factory1, factory2, factory3;
private EmbeddedMediaPlayer mediaPlayer0, mediaPlayer1, mediaPlayer2, mediaPlayer3;