1

I get this exception and I don't know why, since I didn't close any streams. I wrapped the wav files in a BufferedInputStream so i can call input.reset() after playing the sounds, this way I can play them more than once using clip.start(). Where is the stream getting closed?

public static void init() {
    String path = "res/sfx/";

    InputStream audioSrc;
    InputStream bufferedIn;

    try {
        audioSrc = Sound.class.getClassLoader().getResourceAsStream(path + "select.wav");
        bufferedIn = new BufferedInputStream(audioSrc);
        select = AudioSystem.getAudioInputStream(bufferedIn);//exception thrown here

        audioSrc = Sound.class.getClassLoader().getResourceAsStream(path + "hiping.wav");
        bufferedIn = new BufferedInputStream(audioSrc);
        hiPing = AudioSystem.getAudioInputStream(bufferedIn);

        audioSrc = Sound.class.getClassLoader().getResourceAsStream(path + "loping.wav");
        bufferedIn = new BufferedInputStream(audioSrc);
        loPing = AudioSystem.getAudioInputStream(bufferedIn);

        audioSrc = Sound.class.getClassLoader().getResourceAsStream(path + "explode.wav");
        bufferedIn = new BufferedInputStream(audioSrc);
        explode = AudioSystem.getAudioInputStream(bufferedIn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The rest of the exception message is:

at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at com.sun.media.sound.RIFFReader.read(Unknown Source)
at com.sun.media.sound.RIFFReader.<init>(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.internal_getAudioFileFormat(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioFileFormat(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Skywarp
  • 989
  • 3
  • 15
  • 32
  • 1
    You really opened it? Did you check `audioSrc` is not null? Notice that `getResourceAsStream` returns null if the path is incorrect or the file cannot be opened. – leonbloy Sep 01 '16 at 22:55
  • The paths are correct because I was opening them beforehand in previous implementations. The first implementation opened the files each time the clip was played-- it was laggy because of slow disk IO. The second implementation loaded the files into ram, but clip.start() would only play the files once. I tried using input.reset(), but that threw a mark/reset not supported IOException, which was solved in this thread: https://stackoverflow.com/questions/5529754/java-io-ioexception-mark-reset-not-supported The solution in the link it what I am now trying to implement. – Skywarp Sep 01 '16 at 23:03
  • YOu probably are right, but anyway, it's good practice to check that audioSrc is not null – leonbloy Sep 01 '16 at 23:05
  • You know what-- I think you are right because the classpath isn't configured in Eclipse! I wasn't loading with classpaths previously... I will check this now. – Skywarp Sep 01 '16 at 23:05
  • Yes it was the classpaths, I should have checked for null!. Thank you-- If you put it in answer form I can give you credit for the answer. – Skywarp Sep 01 '16 at 23:14

0 Answers0