javaaudioresourcesinputstreamaudioinputstream

AudioInputStream from InputStream ( load from resource directory)


I try to load my sounds from my resource folder when trying out my Application in the IDE.

For images and other stuff that uses InputStreams I use this method:

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

this lets me open an Inputstream of which I can pull Images.

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.

This is my current way to load sounds from external paths:

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();
}
}

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

EDIT : this way of creating a new AudioInputStream by passing an InputStream

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);
}

also throws errors before even running it


Solution

  • this made it work in my above code:

    public JavaSound(String fileName){
        try {
    
            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
            InputStream is = classloader.getResourceAsStream(fileName);
            AudioInputStream sound = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
    
            // load the sound into memory (a Clip)
            clip = AudioSystem.getClip();
            clip.open(sound);
        }
        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);
        }
    
    
    }
    

    just had to start a new bufferedInputStream with my inputStream to have the AudioInputStream... :D still thanks a lot ;)