I want to create the program the creates list of files in directory. When you click the name once it should play file, and when twice dialog appears, which ask you to change the name of sample. The problem is that, when I am trying to change the file name, and the sound is playing, I got exception, that the file cannot be changed, because it is used by another process. I tried to close the stream with close method(ais.close()) but it doesn't help. Could you tell me how to detach file from the process? Here is my play method:
File sample = fileList.get(index);
try {
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(sample);
clip.open(ais);
clip.loop(0);
} catch (LineUnavailableException ex) {
Logger.getLogger(MainList.class.getName()).log(Level.SEVERE, null, ex);
}
The way of playing the wav file is taken from this post: How can I play sound in Java?