Jump to content
  • Advertisement
Sign in to follow this  

[java] Working with sound

This topic is 5214 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

So, I've been poking around and I have a basic idea of how to open a WAV file, open it, play it, close it, start/stop it, etc. While this forms a basic framework for playing sounds, it seems somewhat primitive to me. If I'm playing a game and I trigger, say, 5 explosions during 15 frames, I need some way to manage that stuff. All those explosions use the same sound file, so I don't want to load the sound more than once (I suppose an AudioInputStream will do fine here). If I spawn a new explosion, do I simply create a Clip, play it, and then close and delete it when it's done? Or is there some more efficient way of going about this? And what about managing channels? If I'm playing background music, I don't want an explosion to interrupt it, and I don't want explosions to cut off each other unless I run out of channels on the sound card. I might even perform software mixing to avoid cutting sounds short at all. I know that's a bit muddled, but how do I go about dealing with sound effectively? I've seen half a dozen tutorials about opening an AudioInputStream, creating a Clip, and playing it. But I need to figure out how to deal with multiple sounds, all starting and stopping at different times, etc.

Share this post

Link to post
Share on other sites
I've never had much luck with the Java sound API, it always seems to end up laggy or inefficiant in some way. Perhaps using one of the OpenAL libraries would be a better option?

Share this post

Link to post
Share on other sites
But I need to figure out how to deal with multiple sounds, all starting and stopping at different times, etc.

I have an interface in my game library to manage multiple sounds, maybe it's worth for you to check:
jar library

How to use it:
// import required api
import com.golden.gamedev.engine.BaseAudio;
import com.golden.gamedev.engine.BaseIO;

import com.golden.gamedev.engine.audio.WaveRenderer;
import com.golden.gamedev.engine.audio.MidiRenderer;

// in initialization (constructor), init the audio manager
// the wave interface (for sound effect)
WaveRenderer wave = new WaveRenderer();
// the midi interface (for background music)
MidiRenderer midi = new MidiRenderer();

// the io to pull the sound file
BaseIO io = new BaseIO(TestCase.class); // new BaseIO(getClass());

// the sound manager
BaseAudio soundManager = new BaseAudio(io, wave);
BaseAudio musicManager = new BaseAudio(io, midi);


// in the game
// play background music

// play multiple sound at once

// playing multiple "one.wav"

// play/repeat "four.wav"
// (only one sound could be playing, replay if the sound is already playing)
soundManager.play("four.wav", BaseAudio.SINGLE_REPLAY);

The documentation: audio manager documentation
Don't forget to include the jar in your classpath.

If you find it exactly like you want to, you could ask the source from me or you could ask me the technique

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!