I created a app use javafx+vlcj,play mutiple local videos in a GridPane,16 videos are playing one time (i know it's need much CPU resource), vlcj-4.7.1 && vlcj-javafx-1.0.2 && org.openjfx-14.0.2.1, the demo code like this:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;
public class VideoTest extends Application {
MediaPlayerFactory mediaPlayerFactory;
List<ImageView> imageViewList;
GridPane gridPane;
List<EmbeddedMediaPlayer> embeddedMediaPlayerList = new ArrayList<>();
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
mediaPlayerFactory = new MediaPlayerFactory();
for (int i = 0; i < 16; i++) {
embeddedMediaPlayerList.add(mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer());
}
AnchorPane anchorPane = new AnchorPane();
Button button = new Button("play");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int mVideoWidth = 187;
int mVideoHeight = 331;
for (int j = 0; j < 16; j++) {
ImageView videoImageView = imageViewList.get(j);
videoImageView.setPreserveRatio(true);
embeddedMediaPlayerList.get(j).videoSurface().set(videoSurfaceForImageView(videoImageView));
videoImageView.setFitWidth(mVideoWidth);
videoImageView.setFitHeight(mVideoHeight);
GridPane.setHgrow(videoImageView, Priority.ALWAYS);
gridPane.getChildren().add(videoImageView);
embeddedMediaPlayerList.get(j).media().play("xxx.avi");
}
}
});
gridPane = new GridPane();
ColumnConstraints fillColumn = new ColumnConstraints();
fillColumn.setHgrow(Priority.ALWAYS);
fillColumn.setFillWidth(true);
RowConstraints fillRow = new RowConstraints();
fillRow.setVgrow(Priority.ALWAYS);
fillRow.setFillHeight(true);
int rows = 2;
int cols = 8;
for (int row = 0; row < rows; row++) {
gridPane.getRowConstraints().add(fillRow);
}
for (int col = 0; col < cols; col++) {
gridPane.getColumnConstraints().add(fillColumn);
}
int i = 0;
imageViewList = new ArrayList<>();
for (int j = 0; j < 16; j++) {
imageViewList.add(new ImageView());
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
GridPane.setConstraints(imageViewList.get(i++), col, row);
}
}
AnchorPane.setTopAnchor(gridPane, 100.0);
anchorPane.getChildren().add(gridPane);
anchorPane.getChildren().add(button);
Scene scene = new Scene(anchorPane, 1080, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
}
but sometimes(may be 1 in 3 times) get errors like this often.
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 6
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at javafx.scene.Parent.updateCachedBounds(Parent.java:1704)
at javafx.scene.Parent.recomputeBounds(Parent.java:1648)
at javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501)
at javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
at com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
at com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBoundsImpl(RegionHelper.java:78)
at com.sun.javafx.scene.layout.RegionHelper.superComputeGeomBounds(RegionHelper.java:62)
at javafx.scene.layout.Region.doComputeGeomBounds(Region.java:3289)
at javafx.scene.layout.Region$1.doComputeGeomBounds(Region.java:168)
at com.sun.javafx.scene.layout.RegionHelper.computeGeomBoundsImpl(RegionHelper.java:89)
at com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:115)
at javafx.scene.Node.updateGeomBounds(Node.java:3843)
at javafx.scene.Node.getGeomBounds(Node.java:3805)
at javafx.scene.Node.getLocalBounds(Node.java:3753)
at javafx.scene.Node.updateTxBounds(Node.java:3907)
at javafx.scene.Node.getTransformedBounds(Node.java:3699)
at javafx.scene.Node.updateBounds(Node.java:762)
at javafx.scene.Parent.updateBounds(Parent.java:1835)
at javafx.scene.Parent.updateBounds(Parent.java:1833)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2525)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:412)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:411)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:438)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:563)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:543)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:536)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:342)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
i don't know why? pls help