Sign in to follow this  

[java] Working with sound

This topic is 4856 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
Quote:
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:
[source=java]
// 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
musicManager.play("music.mid");

// play multiple sound at once
soundManager.play("one.wav");
soundManager.play("two.wav");
soundManager.play("three.wav");
soundManager.play("four.wav");

// playing multiple "one.wav"
soundManager.play("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

This topic is 4856 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this