0

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?

Community
  • 1
  • 1
Siocki
  • 53
  • 2
  • 9
  • `Clip clip = AudioSystem.getClip();` declare `Clip clip = null;` as a class attribute, then `clip = AudioSystem.getClip();` in the constructor to ensure that only one clip ever exists. General tips: 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Always copy/paste error and exception output! – Andrew Thompson Mar 24 '15 at 02:50
  • I managed to solve this problem in a different way. I used the internal API property, so I don't want to post my solution(I think you should't do it that way). But I will check Andrew's advice, and if it would work, I would post the solution. – Siocki Mar 26 '15 at 07:20

1 Answers1

0

Make sure that you close() clip, too.

Sanny
  • 303
  • 3
  • 9