多线定制电池厂

时间:2017-10-13 17:11:13

标签: java multithreading javafx-8 cell controlsfx

我目前有一个自定义单元工厂,其中包含通过javafx.scene.media播放的视频。问题是当我添加多个视频时,应用程序是滞后的。

如何将每个单元格放在自己的线程上?是否有更好的方法将视频添加到单元格中,每个视频都有自己的线程?

CrontrolsFX Custom Cell Factory

gridView.setCellFactory(new Callback<GridView<MediaCard>, GridCell<MediaCard>>() {

        @Override
        public GridCell<MediaCard> call(GridView<MediaCard> param) {
            // TODO Auto-generated method stub
            return new MediaCell();
        }

    });

MediaCell.java

public class MediaCell extends GridCell<MediaCard>{

public MediaCell() {
    super();
}

@Override
protected void updateItem(MediaCard item, boolean empty) {

    if (item != null) {

        super.updateItem(item, empty);

        AnchorPane rootAnchorPane = new AnchorPane();

        File f = new File(item.getPath());

        Media media = new Media(f.toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.play();

        MediaView mv = new MediaView(mediaPlayer);
        mv.setPreserveRatio(true);

        Button viewMediaButton = new Button("View");

        VBox vbox = new VBox(mv, viewMediaButton);

        DoubleProperty mvw = mv.fitWidthProperty();
        DoubleProperty mvh = mv.fitHeightProperty();
        mvw.bind(Bindings.selectDouble(mv.parentProperty(), "width"));
        mvh.bind(Bindings.selectDouble(mv.parentProperty(), "height"));

        rootAnchorPane.getChildren().addAll(vbox);  
        AnchorPane.setTopAnchor(vbox, 0.0);
        AnchorPane.setBottomAnchor(vbox, 0.0);
        AnchorPane.setLeftAnchor(vbox, 0.0);
        AnchorPane.setRightAnchor(vbox, 0.0);

        setGraphic(rootAnchorPane);

    }
}

}

尝试将每个单元格移动到自己的线程

失败
gridView.setCellFactory(new Callback<GridView<MediaCard>, GridCell<MediaCard>>() {

        @Override
        public GridCell<MediaCard> call(GridView<MediaCard> param) {
            // TODO Auto-generated method stub

            new Thread(new Runnable() {

                @Override
                public void run() {
                    mediaCell = new MediaCell();
                }
            }).start();
            return mediaCell;
        }

    });

1 个答案:

答案 0 :(得分:0)

您只需将mediaPlayer.play()放入新的线程

即可

类似的东西:

public class MediaCell extends GridCell<MediaCard> {

    public MediaCell() {
        super();
    }

    @Override
    protected void updateItem(MediaCard item, boolean empty) {

        if (item != null) {

            super.updateItem(item, empty);

            AnchorPane rootAnchorPane = new AnchorPane();

            File f = new File(item.getPath());

            Media media = new Media(f.toURI().toString());
            MediaPlayer mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setAutoPlay(true);

            new Thread() {
                @Override
                public void run() {
                    mediaPlayer.play();
                }
            }.start();

            MediaView mv = new MediaView(mediaPlayer);
            mv.setPreserveRatio(true);

            Button viewMediaButton = new Button("View");

            VBox vbox = new VBox(mv, viewMediaButton);

            DoubleProperty mvw = mv.fitWidthProperty();
            DoubleProperty mvh = mv.fitHeightProperty();
            mvw.bind(Bindings.selectDouble(mv.parentProperty(), "width"));
            mvh.bind(Bindings.selectDouble(mv.parentProperty(), "height"));

            rootAnchorPane.getChildren().addAll(vbox);  
            AnchorPane.setTopAnchor(vbox, 0.0);
            AnchorPane.setBottomAnchor(vbox, 0.0);
            AnchorPane.setLeftAnchor(vbox, 0.0);
            AnchorPane.setRightAnchor(vbox, 0.0);

            setGraphic(rootAnchorPane);

        }
    }
}