Paulscode loading from InputStream?

Is it possible to load a sound and play it from an inputstream? I want to be able to access from within and outside the jar.

Yes, it is, I do that in the pre-beta version of TUER. I use Paul Lamb Sound Library with its JOAL plugin.

Yes, as gouessej say, can do with Paul’s library.
Can also be done with TinySound.
Can also be done with straight javax.sound.sampled.

However, in the latter case, you use AudioInputStream, not InputStream. InputStream has requirements for the file (e.g., Marking, Resetting) that are usually not met by audio data files.

I’m assuming you just meant input streaming in a general sense, weren’t referring to the specific Java class InputStream.

InputStream methods mark() and reset() only have to provide their described functionality, if markSupported() returns true, which by default it does not.

AudioInputStream is part of javax.sound.sampled. I can’t recall how Paul has us load sounds. It’s been a couple years since I looked at his library. I’ve just been using methods based on javax.sound.sampled.

I’ve been wondering, too, how to do what you are asking about, using LWJGL or LibGDX. My system is too slow to implement Android emulation, so I haven’t tested sound coding in that context. But I also wonder if LWJGL & Libgdx make use of javax.sound.sampled for their implementation. I recall someone writing somewhere that this was true, but my memory is shaky and I could have it totally wrong.

Looking at this tutorial: http://www.lwjgl.org/wiki/index.php?title=OpenAL_Tutorial_1_-_Single_Static_Source
something called WaveData is used for loading a sound. By its syntax, it seems to me you could load either from within the jar or not, according to the usual rules of addressing files. But I haven’t tested it.

Ah, I was being sloppy. Further clarifying, according to the comments in the api for AudioSystem.getAudioInputStream(InputStream inputStream) http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream(java.io.InputStream), this method for loading a file may or may not run the tests Riven mentions. For this reason, I use a URL for the input parameter instead of an InputStream. But some folks prefer to wrap the InputStream in a BufferedInputStream, and this also works.

I’d consider the implementers of [icode]getAudioInputStream(InputStream stream)[/icode] sloppy. They should simply have wrapped their InputStream parameter in a BufferedInputStream as part of their implementation, and not put the burden on the callsite. I mean, java.awt.image.ImageIO also uses multiple parses/loaders to make an attempt at loading the file, and does this marking/reseting behind the scenes.

Class.getResource() returns a URL that you can pass to paulscode.sound.SoundSystem.loadSound(URL,String). I use this method to load my sound samples from JARs.

If you know the location of the jar, you can just use it directly.

If not, the following is one technique:

System.getProperty("user.dir");

Nice list of various properties here:
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties()

Here is another method:

     URL url = Location.class.getProtectionDomain()
        .getCodeSource().getLocation();

I’ve never tried it, but when I did a search, this came up numerous times.
(Search I did was: java find jar location)

With file access outside of the jar, there may be permission issues.


public static String jarDir()
{
	try
	{
		return (new File(SomeClass.class.getProtectionDomain().getCodeSource().getLocation().toURI())).getParent();
	}
	catch(URISyntaxException e)
	{
		e.printStackTrace();
		return null;
	}
}

Will return the directory containing the jar file.