I just made an application with javafx that cycles videos in a folder.
I had to set the next video firing the setonendofmedia event, so they are in a cycle, but the problem is that the application loads every video at the beginning, so after a while it fills memory and crashes.Is there another way to cycle videos without pre-load them or a way to flush memory every while?
My Code :
package
application;
import
java.io.File;
import
java.io.FilenameFilter;
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.List;
import
javax.swing.JOptionPane;
import
javafx.application.Platform;
import
javafx.geometry.Pos;
import
javafx.scene.Scene;
import
javafx.scene.layout.StackPane;
import
javafx.scene.layout.VBox;
import
javafx.scene.media.MediaView;
import
javafx.scene.media.MediaPlayer;
import
javafx.scene.media.Media;
class
SceneGenerator {
private
int xSize;
private
int ySize;
private
MediaPlayer playerNew;
private
int nextFileIndex =
-1
;
private
List<
String
> videoFiles;
private
MediaView mediaView;
Runnable finale;
public
SceneGenerator(int xSize,int ySize){
this
.xSize=xSize;
this
.ySize=ySize;
finale =
new
Runnable() {
@Override
public
void run() {
esegui();
}
};
}
public
Scene createScene() {
final StackPane layout =
new
StackPane();
final File dir =
new
File(System.getProperty(
"user.dir"
)+
"\\video"
);
if
(!dir.exists() || !dir.isDirectory()) {
JOptionPane.showMessageDialog(
null
,
"Cannot find video source directory: "
+ dir);
Platform.exit();
System.exit(
0
);
return
null
;
}
mediaView =
new
MediaView();
mediaView.setPreserveRatio(
false
);
mediaView.setFitHeight(ySize-((ySize/
100
)*(JavaPlayer.panelSouthYDimension+
1
)-JavaPlayer.pixelAdattamento));
mediaView.setFitWidth(xSize);
videoFiles =
new
ArrayList<
String
>();
for
(
String
file : dir.list(
new
FilenameFilter() {
@Override
public
boolean accept(File dir,
String
name) {
return
name.endsWith(
".mp4"
)||name.endsWith(
".flv"
);
}
})) videoFiles.add(
"file:///"
+ (dir +
"/"
+ file).
replace
(
"\\"
,
"/"
)
);
Collections.sort(videoFiles);
for
(int kk=
0
;kk<videoFiles.size();kk++){
System.out.println(videoFiles.get(kk));
}