2

I am aware that this is possibly a duplicate. I've checked the other questions before and did not succeed in solving my problem. I am trying to build some sort of instant button app with javafx. At some point I play a file with the following code:

if(mediaPlayer != null){
    mediaPlayer.stop();
}
File soundFile = new File(Config.getInstantSoundsDirectory() + File.separator + fileName);
Media mediaFile = new Media(soundFile.toURI().toString());
mediaPlayer = new MediaPlayer(mediaFile);        
mediaPlayer.setOnEndOfMedia(() -> mediaPlayer.dispose());
mediaPlayer.play();

If I want to delete the file I am using following code:

try {
    Files.deleteIfExists(Paths.get(Config.getInstantSoundsDirectory(), fileName));
} catch (IOException e1) {      
    e1.printStackTrace();
} 

It works if I've never played the file before. If I did though, I will get an exception that says I can't delete it because some other process is using it. According to other posts the solution to remove the file handle from the file is to call dispose() on my mediaPlayer, as I am doing right when the file was played. Unfortunately the error persists. Am I maybe missing something ?

Thanks for your help.

Greets Ionic

Edit -> Here is my sample test code:

String filePath = Paths.get(".", "test.mp3").toString();
File soundFile = new File(filePath);
Media hit = new Media(soundFile.toURI().toString());
player = new MediaPlayer(hit);
player.setOnEndOfMedia(() -> {
    player.dispose();
    try {
        Files.deleteIfExists(Paths.get(".", "test.mp3"));
    } catch (IOException e) {       
        e.printStackTrace();
    }
});
player.play();
IonicBlaze
  • 125
  • 1
  • 11
  • When are you attempting to delete the file? By the way, you don’t need File.separator. You can simply write `Paths.get(Config.getInstantSoundsDirectory(), fileName)`. – VGR Oct 05 '16 at 21:36
  • I added a ContextMenu which has a MenuItem that triggers the delete. Thanks for the hint with the seperator. The media will always play to the end. If I right click on a button and click the delete item in my ContextMenu this is the moment the code for deletion executes. – IonicBlaze Oct 05 '16 at 21:45
  • And are you activating that MenuItem before the end of media has been reached, or after? – VGR Oct 05 '16 at 21:48
  • Always after end of media has been reached. – IonicBlaze Oct 05 '16 at 21:49
  • I wrote a simple test and had no trouble deleting a file after calling MediaPlayer.dispose() in Windows 7. Is it possible another process, or another part of your program, has the file open? – VGR Oct 05 '16 at 22:08
  • Hm, I am very sure that nothing else accesses the file. I am on Win10, does this make any difference ? – IonicBlaze Oct 05 '16 at 22:10
  • I also wrote a simple test, just the bare minimum (Creating a media with the file, playing it with MediaPlayer, disposing after finish and trying to delete).. I still get the error. – IonicBlaze Oct 05 '16 at 22:17
  • Edit your question to include the code for your simple test. It will serve as an [MCVE](http://stackoverflow.com/help/mcve) and will help others troubleshoot your problem. – VGR Oct 05 '16 at 22:57
  • I added a simple example that fails at the point where I want to delete the file, just like the original situation – IonicBlaze Oct 06 '16 at 13:08
  • 1
    [This question](http://stackoverflow.com/questions/31606978/odd-behaviour-when-deleting-files-with-files-delete) is sort of the opposite of your issue, but I suspect it’s relevant: It seems Windows does not always perform file handle operations right away. As ugly as it is, I would try a heuristic sleep (and probably a retry loop). Caution: *do not* sleep in an event handler; it will hang your application. Do it in a new thread. – VGR Oct 06 '16 at 13:43
  • 1
    I had such a loop running for 10 minutes now, without success. I assume that the file handle is somehow bound to the running JVM. As soon as the JVM exits I can delete the file, regardless of how much time passed between opening the file and shutting down the JVM. I think I will just mark that file for deletion so that it will be deleted the next time my program starts. Thanks for your help, I appreciate it. – IonicBlaze Oct 06 '16 at 13:59

0 Answers0