So, I've searched around quite a bit, and haven't found anything that resolves this. (found someone asking in 2016, with no answers and did it somewhat differently, also I've found some that didn't care if the file was deleted on exit, or on next start up, which I do)
I have an application where I have one button for letting a user pick an mp3 file on the computer, and one button that let's the user play that sound back, when the user is done, he/she can click a save button that is supposed to delete the mp3 file that is stored in the user's folder, and then replace it with the new one the user picked, they are supposed to do this continuously, so deleting the previous file on exit isn't very good, as it could lead to a big heap of temp files that needs to be deleted on exit, and checked for on launch.
so, from what I can gather it seems to be a Windows-centric problem where a file opened in mediaplayer isn't released properly(or something like that, was a bit hard to follow..), so I was wondering if there is a way to force the MediaPlayer object to release the Media object/file, or maybe a way to find out and have a listener for when the dispose() method is done doing it's thing, so that I can delete the file while the java program continues to run afterwards?
here is a code snippet to illustrate the problem.
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main extends Application {
public static void main(String[] args){launch(args);}
@Override
public void start(Stage primaryStage) throws Exception {
Path path = Paths.get("some/file/path.mp3"); //making a path for the Media object and for deleting later
Media media = new Media(path.toUri().toString()); //Making a Media object for the Mediaplayer
MediaPlayer mediaPlayer = new MediaPlayer(media); //Making a MediaPlayer
mediaPlayer.play(); //playing just to make sure it has been used
mediaPlayer.stop(); //stopping the player
mediaPlayer.dispose(); //disposing of the MediaPlayer
//checking to see that the MediaPlayer has been disposed of at least
try {
if (mediaPlayer.getStatus().equals(MediaPlayer.Status.DISPOSED)) {
Files.delete(path); //trying to delete, and subsequently crashing..
}
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0); //terminating program
}
}
edit(added the exception thrown(replacing my actual path with the dummy one)):
java.nio.file.FileSystemException: some/file/path.mp3: The process cannot access the file because it is being used by another process.
my current thought on a solution is to have a file where I mark files for deletion when the program starts next, and keeping track of what files should be copied over to the user folder then.
or maybe even not actually saving the files in the users folder, and rather keeping a reference to the users files in a text file or something or other, while I just have a general "sounds folder" that I can get things from..
or something like that, but I thought I'd at least ask you people if you had any ideas on how to solve it as I initially wanted to do it.
Really sorry if this should end up as a duplicate, I haven't really found any answers that have helped when searching for this, but if you do, then please send me on my way over to wherever that is :)
Thank you for any Ideas you might have, and hope the English is understandable, also, if you want any more info or anything, please let me know :)
Edit 2:
So, I've found a "bad" workaround, where I use the AudioInputStream to play a sound with a Clip, as here I can control the stream, and close it before deleting the file, this seems to work quite well, only problem with this is that I am now limited to wav files(afaik), but I can work with that for now at least, but not leaving it as an answer, as a MediaPlayer solution would be far better imo.
Edit 3(forgot to add the code)(got the audio part from here: https://stackoverflow.com/a/11025384/10044355):
Path path = Paths.get("some/file/path.wav");
File yourFile = new File(path.toString());
AudioInputStream stream = null;
AudioFormat format;
DataLine.Info info;
Clip clip;
try {
stream = AudioSystem.getAudioInputStream(yourFile);
}catch (Exception e){
e.printStackTrace();
}
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
clip.stop();
stream.close();
Files.delete(path);