VLCJ 播放器在 JavaFX 场景中崩溃

VLCJ player crashing in a JavaFX Scene

我需要一个 JavaFX 场景中的多个播放器,使用 FXML,来做一个 CCTV 监控系统,显示实时摄像机流。

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import java.net.URL;
import java.util.ResourceBundle;

import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;


public class allCamerasMainGridScreenController implements Initializable {
    public MediaPlayerFactory mediaPlayerFactory;
    public  EmbeddedMediaPlayer embeddedMediaPlayer;

    @FXML BorderPane mainBorderPane;
    @FXML GridPane cameraViewGrid;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
            ImageView cameraView = new ImageView();
            mediaPlayerFactory = new MediaPlayerFactory();
            embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
            cameraView.setPreserveRatio(true);
            cameraView.setFitHeight(340);
            embeddedMediaPlayer.videoSurface().set(videoSurfaceForImageView(cameraView));
            cameraViewGrid.add(cameraView,0,0);
            embeddedMediaPlayer.media().play("http://10.0.0.6:8081");
    }

}

使用这段代码我可以获得一个播放器 运行,但它在几秒钟后崩溃,在 IntelliJ 上抛出此消息

Process finished with exit code -1073741819 (0xC0000005)

评论说控制器需要硬引用,我想我能妥善解决这个问题。

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import java.net.URL;
import java.util.ResourceBundle;

import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;


public class mainGridScreenController implements Initializable {
    public MediaPlayerFactory mediaPlayerFactory;
    public  EmbeddedMediaPlayer embeddedMediaPlayer;

    @FXML BorderPane mainBorderPane;
    @FXML GridPane cameraViewGrid;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //Just by adding this two lines the controller isn't elegible for garbage collection anymore
        mainGridController mainGridController = new mainGridController();
        mainBorderPane.setUserData(this);


        ImageView cameraView = new ImageView();
        mediaPlayerFactory = new MediaPlayerFactory();
        embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
        cameraView.setPreserveRatio(true);
        cameraView.setFitHeight(340);
        embeddedMediaPlayer.videoSurface().set(videoSurfaceForImageView(cameraView));
        cameraViewGrid.add(cameraView,0,0);
        embeddedMediaPlayer.media().play("http://10.0.0.6:8081");
    }

}