AudioInputStream from InputStream ( load from resource directory)
<p>I try to load my sounds from my resource folder when trying out my Application in the IDE.</p>

<p>For images and other stuff that uses InputStreams I use this method:</p>

<pre><code>@Override
public InputStream readAsset(String fileName) throws IOException {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream(fileName);
    return is;
}
</code></pre>

<p>this lets me open an Inputstream of which I can pull Images.</p>

<p>As soon as I would try to cast this InputStream to an Audio InputStream I get errors. Also if I would try to make a new AudioInputStream passing the above InputStream as the parameter.</p>

<p>This is my current way to load sounds from external paths:</p>

<pre><code>public class JavaSound implements Sound {

private Clip clip;


public JavaSound(String fileName){
    try {
        File file = new File(fileName);
        if (file.exists()) {

            //for external storage Path
            AudioInputStream sound = AudioSystem.getAudioInputStream(file);

            // load the sound into memory (a Clip)
            clip = AudioSystem.getClip();
            clip.open(sound);
        }
        else {
            throw new RuntimeException("Sound: file not found: " + fileName);
        }
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
        throw new RuntimeException("Sound: Malformed URL: " + e);
    }
    catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
        throw new RuntimeException("Sound: Unsupported Audio File: " + e);
    }
    catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Sound: Input/Output Error: " + e);
    }
    catch (LineUnavailableException e) {
        e.printStackTrace();
        throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
    }


}

@Override
public void play(float volume) {

    // Get the gain control from clip
    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

    // set the gain (between 0.0 and 1.0)
    float gain = volume;
    float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
    gainControl.setValue(dB);

    clip.setFramePosition(0);  // Must always rewind!
    clip.start();
}

@Override
public void dispose() {
    clip.close();
}
}
</code></pre>

<p>how can i exchange the AudioInputStream part to work like the first code, pulling the files out of my resource directory?</p>

<p>EDIT :
this way of creating a new AudioInputStream by passing an InputStream</p>

<pre><code>File file = new File(fileName);
        if (file.exists()) {
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            InputStream is = classloader.getResourceAsStream(fileName);

            //for external storage Path
            AudioInputStream sound = new AudioInputStream(is);

            // load the sound into memory (a Clip)
            clip = AudioSystem.getClip();
            clip.open(sound);
}
</code></pre>

<p>also throws errors before even running it</p>