VLCJ上的多个流

问题描述 投票:0回答:1

所以,我有这个代码

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个factorys和4个媒体播放器,但我不认为这是正确的方法。我想了解我所缺少的东西。谢谢。

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at core.MultiThreadedWindow.<init>(MultiThreadedWindow.java:56)
java vlcj
1个回答
0
投票

您在第56行看到了NPE,因为您尚未在第10行初始化您的阵列(就此而言是9)。

另一个代码使用类成员而不是数组来保存它们。

private MediaPlayerFactory factory0, factory1, factory2, factory3;
private EmbeddedMediaPlayer mediaPlayer0, mediaPlayer1, mediaPlayer2, mediaPlayer3;
© www.soinside.com 2019 - 2024. All rights reserved.