Jump to content
  • Advertisement
Sign in to follow this  

[java] Sound issue - javax.sound.sampled

This topic is 4219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

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

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 this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!