I am working on a media player application and I want to delete the decrypted media file which is created in a specific folder after running the encrypted media file as soon as the JVM terminates. I tried using deleteOnExit(); function but after three attempts it doesn't delete the file. Why might this be happening? Below is the code I am using to delete the file
@FXML MediaView mv;
MediaPlayer mp;
Media me;
@FXML Slider volumeslider;
@FXML Slider seekslider;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
String key = "Mary has one cat";
File inputFile = new File("C:\\Users\\Administrator\\Downloads\\video\\ec.mp4");
String path=new File("C:\\Users\\Administrator\\Downloads\\video\\dc.mp4").getAbsolutePath();
try {
CryptoUtils.decrypt(key, inputFile, path);
} catch (CryptoException ex) {
ex.printStackTrace();
}
me=new Media(new File(path).toURI().toString());
mp=new MediaPlayer(me);
mv.setMediaPlayer(mp);
mp.setAutoPlay(true);
DoubleProperty width= mv.fitWidthProperty();
DoubleProperty height= mv.fitHeightProperty();
width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
volumeslider.setValue(mp.getVolume() * 100);
volumeslider.valueProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
mp.setVolume(volumeslider.getValue() / 100);
}
});
mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
seekslider.setValue(newValue.toSeconds());
seekslider.maxProperty().bind(Bindings.createDoubleBinding(
() -> mp.getTotalDuration().toSeconds(),
mp.totalDurationProperty()));
File file = new File(path);
file.deleteOnExit();
file.delete();
file = null;
}
});
seekslider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mp.seek(Duration.seconds(seekslider.getValue()));
mp.setOnEndOfMedia(new Runnable() {
public void run() {
mp.seek(Duration.minutes(1));
}
});
}
});
}catch(Exception e) {
}
mv.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
mp.play();
mp.pause();
}
});
}
public void play(ActionEvent event) {
mp.play();
//mp.setRate(1);
mp.pause();
}