Meaningless Drivel is fun!*
The moose likes JavaFX and the fly likes Can't delete file when using MediaPlayer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "Can Watch "Can New topic
Author

Can't delete file when using MediaPlayer

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
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

Joined: May 03, 2008
Posts: 4523
    
    5

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

luck, db
There are no new questions, but there may be new answers.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Apr 11, 2005
Posts: 744
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't delete file when using MediaPlayer
 
Similar Threads
Reading an mp3 file
Executing VLC in Java swing
Problem with ImageIO.write
Delete file
how to enable Drag and Drop in my media player