# [java] Sound issue - javax.sound.sampled

## Recommended Posts

CodeMachine    100
Hi I have some troubles loading/playing sounds. In my game, I have a lot of sounds that could be played several times after eachother in a short interval of time. When starting the game, I load my sounds like below. I then save the clip-object in a vector for later retrieval.
AudioInputStream s = AudioSystem.getAudioInputStream("/sound.wav");
AudioFormat f = s.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, s.getFormat(), ((int)s.getFrameLength()*f.getFrameSize()));
clip = (Clip)AudioSystem.getLine(info);
clip.open(stream);


When playing a sound I set the frame position to the start position. This simply stops the previous playing sound if the same instance is called.
Clip c = _vectorSounds.get("sound.wav");
c.setFramePosition(0);
c.start();


How do I load each sound once into memory and then play it more than once in parallell? (A maximum of 5 instances of each sound could be playing in parallell.) Kind Regards

##### Share on other sites
CodeMachine    100
Ok, I guess this question was too complicated :) :/

##### Share on other sites
capn_midnight    1707
if you want one sound to be playing multiple times overlapping (for example, a 3 second sound file played twice, one starting 1 second after the first), then you need to create seperate Clips for each. A Clip is a line of audio input, not an actual sound file. So, multiple Clips per sound file will allow you to play overlaaping sounds in the way you want.

I keep the Clips around, and I iterate through a list of clips per sound file. If I find a clip that isn't currentlly playing, then I set it to play. If I don't find a clip that is playing, then I create a new clip and add it to the list of clips for that specific sound file. It works pretty well so far.