public final class SoundPool
- Object
- SoundPool
Plays many short, overlapping sound effects with low latency.
A SoundPool is built for game audio: gunshots, coins, footsteps – sounds that
must trigger instantly and play several at once. Load each clip once with
#load(String) and trigger it repeatedly with #play(SoundEffect); the pool
mixes up to #getMaxStreams() voices simultaneously and drops the request
(returning -1) rather than blocking when that limit is reached.
SoundPool sfx = SoundPool.create(8);
SoundEffect coin = sfx.load("/coin.wav");
// ... in the game loop:
coin.play();
On platforms with a purpose built low latency audio engine (Android, iOS, the
desktop simulator and the browser) the pool uses it directly, supporting per
play volume, stereo pan and pitch/rate. Where no native backend exists it falls
back to a com.codename1.media.MediaManager based pool that still works
everywhere but has higher latency and ignores pan and rate –
#isNativeAccelerated() reports which path is in use.
Methods
public static SoundPool create(int maxStreams) | Creates a sound pool that mixes up to maxStreams voices at once. |
public boolean isNativeAccelerated() | True if a native low latency audio backend is in use; false if the cross platform MediaManager fallback is in use. |
public boolean isVoiceCompletionSupported() | True if this pool can report voice completion to a VoiceListener. |
public void setVoiceListener(VoiceListener listener) | Registers a listener notified (on the EDT) as each voice finishes playing, or null to clear it. |
public int getMaxStreams() | The maximum number of voices that can play simultaneously. |
public SoundEffect load(String uri)
throws IOException | Loads a sound effect from a uri (for example a /sound.wav resource path). |
public SoundEffect load(InputStream data, String mimeType)
throws IOException | Loads a sound effect from a stream of the given mime type. |
public AsyncResource<SoundEffect> loadAsync(String uri) | Loads a sound effect from a uri on a background thread, completing the returned resource on success or error. |
public int play(SoundEffect effect) | Plays the effect once at full volume, centered, normal rate. |
public int play(SoundEffect effect, float volume, float pan, float rate, int loop) | Plays the effect with explicit parameters. |
public void setVolume(int voiceId, float volume) | Sets the volume (0.0 to 1.0) of a playing voice. |
public void setRate(int voiceId, float rate) | Sets the playback rate / pitch of a playing voice (native backends only). |
public void setPan(int voiceId, float pan) | Sets the stereo pan (-1.0 to 1.0) of a playing voice (native backends only). |
public void pause(int voiceId) | Pauses a playing voice. |
public void resume(int voiceId) | Resumes a paused voice. |
public void stop(int voiceId) | Stops a voice. |
public void stopAll() | Stops every playing voice. |
public void autoPause() | Pauses all playback, for example when the app is backgrounded. |
public void autoResume() | Resumes playback paused by #autoPause(). |
public void release() | Releases the pool and all loaded effects. |
Inherited methods
Method details
create
public static SoundPool create(int maxStreams)maxStreams voices at once.isNativeAccelerated
public boolean isNativeAccelerated()MediaManager fallback is in use.isVoiceCompletionSupported
public boolean isVoiceCompletionSupported()VoiceListener. The
cross-platform fallback mixer supports it; some native engines (e.g. Android’s
SoundPool) do not.setVoiceListener
public void setVoiceListener(VoiceListener listener)null to clear it. Has no effect on backends where
#isVoiceCompletionSupported() is false.getMaxStreams
public int getMaxStreams()load
public SoundEffect load(String uri)
throws IOException/sound.wav resource path).Throws
load
public SoundEffect load(InputStream data, String mimeType)
throws IOExceptionThrows
loadAsync
public AsyncResource<SoundEffect> loadAsync(String uri)play
public int play(SoundEffect effect)play
public int play(SoundEffect effect, float volume, float pan, float rate, int loop)Parameters
effectSoundEffect- the loaded sound to play
volumefloat- 0.0 (silent) to 1.0 (full)
panfloat- -1.0 (full left) to 1.0 (full right), 0.0 centered (ignored by the fallback)
ratefloat- playback rate / pitch, 1.0 normal, typically 0.5 to 2.0 (ignored by the fallback)
loopint- 0 plays once, -1 loops forever, n repeats n extra times
Returns
#stop(int) etc., or -1 if the pool is exhaustedsetVolume
public void setVolume(int voiceId, float volume)setRate
public void setRate(int voiceId, float rate)setPan
public void setPan(int voiceId, float pan)pause
public void pause(int voiceId)resume
public void resume(int voiceId)stop
public void stop(int voiceId)stopAll
public void stopAll()autoPause
public void autoPause()autoResume
public void autoResume()#autoPause().release
public void release()