Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't delete file when using MediaPlayer

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, when I use javafx.scene.media.MediaPlayer to play a music file (mp3/wav), I am physically unable to delete the file (after stopping the music).

It just says the JVM is still using it.
Of course if I quit the program I can delete, but I really need to have the delete functionality whilst program is running.
I've looked a little at the API for javafx.scene.media.MediaPlayer and javafx.scene.media.Media.
Similarly to when reading/writing to file, one should always close any streams that are still open, but I can't seem to flush or close music file playing with MediaPlayer.

Please let me know if anyone can help, thanks
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok sure, I've made as small an example as I can..

Viewers will see I put 2 identical files in:
Kalimba1.mp3 and Kalimba2.mp3

Kalimba1.mp3 is loaded into MusicPlayer to be played
Kalimba2.mp3 is to demonstrate the delete functionality
The program works fine like this.

The issue is when we ignore Kalimba2.mp3 and try to delete Kalimba1.mp3,
simply rename file2 to file1 in delete() method.




You may have to change the path to the file if you want to test my code.
Thanks for any help.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem. It appears that the FX runtime holds on to a reference to the MediaPlayer, which in turn holds on the the Media which doesn't release the File.

Sorry, I couldn't discover a workaround. Maybe setting the File to deleteOnExit() would work, but that's known be not 100% reliable in any case.

You could search the bug database and/or submit a feature request. You'll find how to do that in an announcement at the top of the JavaFX 2.0 and Later topic listing on the other forum you posted in.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks anyway.
I might have to try using temporary files somehow. I need to be able to overwrite the music files while program runs,
so deleting at the end doesn't help much anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic