1 Reply Latest reply on Dec 6, 2010 6:09 PM by captfoss

    WAV file delete error

    821260
      Hello,

      I have written an application which has the feature to delete wav files.

      If the file is created then deleted, it all workds fine and the deletion is successful. However if the file is put through a 'play' method then deleted, then the deletion fails.

      I am unsure why this happens, so was hoping for some insight into the problem

      as for example code, here is the deletion method
      try {
      
                      if (toDelete.delete()) {
                          System.out.println(toDelete.getName() + " is deleted!");
                      } else {
                          System.out.println("Delete operation failed.");
                      }
      and here is the current play method
          public void playFile(File playFile) {
              try {
                  AudioFormat audioFormat;
                  AudioInputStream audioInputStream;
                  SourceDataLine sourceDataLine;
                  audioInputStream = AudioSystem.getAudioInputStream(playFile);
                  audioFormat = audioInputStream.getFormat();
      
                  int length = (int) (audioInputStream.getFrameLength() * audioFormat.getFrameSize());
      
                  byte[] data = new byte[length];
                  DataInputStream is = new DataInputStream(audioInputStream);
                  try {
                      is.readFully(data);
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  }
                  InputStream byteArrayInputStream = new ByteArrayInputStream(data);
                  audioInputStream = new AudioInputStream(byteArrayInputStream, audioFormat, data.length / audioFormat.getFrameSize());
                  DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
                  sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
                  sourceDataLine.open(audioFormat);
                  sourceDataLine.start();
                  int cnt;
                  int totalBytes = (int) (audioInputStream.getFrameLength() * audioInputStream.getFormat().getFrameSize());
                  byte[] tempBuffer = new byte[totalBytes];
                  while ((cnt = audioInputStream.read(
                          tempBuffer, 0,
                          tempBuffer.length)) != -1) {
                      if (cnt > 0) {
      
                          sourceDataLine.write(tempBuffer, 0, cnt);
                      }
                  }
      
                  sourceDataLine.drain();
                  sourceDataLine.close();
                  sourceDataLine = null;
      
              } catch (LineUnavailableException ex) {
                  ex.printStackTrace();
              } catch (UnsupportedAudioFileException ex) {
                  ex.printStackTrace();
              } catch (IOException ex) {
                  ex.printStackTrace();
              }
      
      
          }
      as I am trying to understand this new functionality, the play method is a mish-mash of my own code and online examples. so it would not suprise me if here is where the root of the problem is, but i cant see it hence the need for this post

      thanks in advance for any help!

      K
        • 1. Re: WAV file delete error
          captfoss
          First off, the first 30 lines or so of your playFile function could be replaced by the following single line:
                      AudioFormat audioFormat;
                      AudioInputStream audioInputStream;
                      SourceDataLine sourceDataLine;
                      audioInputStream = AudioSystem.getAudioInputStream(playFile);
                      audioFormat = audioInputStream.getFormat();
           
                      int length = (int) (audioInputStream.getFrameLength() * audioFormat.getFrameSize());
           
                      byte[] data = new byte[length];
                      DataInputStream is = new DataInputStream(audioInputStream);
                      try {
                          is.readFully(data);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                      }
                      InputStream byteArrayInputStream = new ByteArrayInputStream(data);
                      audioInputStream = new AudioInputStream(byteArrayInputStream, audioFormat, data.length / audioFormat.getFrameSize());
          becomes
          AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(playFile);
          And that'll likely fix the delete problem, which I would assume is a result of your code not properly closing all of the input streams (IE, you can't delete the file because it has an active readlock on it)