public interface SoundPoolPeer

Known subtypesCompletionAwareSoundPoolPeer

Low level service provider interface backing com.codename1.gaming.SoundPool.

A peer owns a fixed number of simultaneous playback “voices” and a set of loaded sounds. Each platform port provides its own peer over the platform’s purpose built low latency audio API (Android SoundPool, iOS AVAudioEngine, the desktop javax.sound.sampled mixer, WebAudio in the browser). When a port does not provide one, com.codename1.gaming.SoundPool falls back to MediaSoundPoolPeer, which is implemented purely on top of the existing MediaManager.

A loaded sound is represented by an opaque Object handle returned from #loadSound(String) / #loadSound(InputStream, String). A playing voice is represented by an int id returned from #play(Object, float, float, float, int); -1 means no voice was available. Per voice operations are no-ops if the voice has already finished and been recycled.

Callbacks from the underlying audio engine may arrive off the Codename One EDT; implementations must keep their own bookkeeping thread safe.

Methods

public abstract Object loadSound(InputStream data, String mimeType) throws IOExceptionLoads a short sound from a stream, decoding/buffering it up front so that playback latency is paid here rather than at #play.
public abstract Object loadSound(String uri) throws IOExceptionLoads a short sound from a uri (for example a jar:// resource path).
public abstract int play(Object sound, float volume, float pan, float rate, int loop)Plays a loaded sound, returning a voice id or -1 if the pool is exhausted.
public abstract void setVolume(int voiceId, float volume)
public abstract void setRate(int voiceId, float rate)
public abstract void setPan(int voiceId, float pan)
public abstract void pauseVoice(int voiceId)
public abstract void resumeVoice(int voiceId)
public abstract void stopVoice(int voiceId)
public abstract void stopAll()Stops every currently playing voice.
public abstract void autoPause()Pauses all active playback (for example when the app is sent to the background).
public abstract void autoResume()Resumes playback paused by #autoPause().
public abstract void unloadSound(Object sound)Releases a single loaded sound and its buffers.
public abstract void release()Releases the whole pool and all loaded sounds.

Method details

loadSound

public abstract Object loadSound(InputStream data, String mimeType) throws IOException
Loads a short sound from a stream, decoding/buffering it up front so that playback latency is paid here rather than at #play. The stream is fully consumed and closed.

loadSound

public abstract Object loadSound(String uri) throws IOException
Loads a short sound from a uri (for example a jar:// resource path).

play

public abstract int play(Object sound, float volume, float pan, float rate, int loop)
Plays a loaded sound, returning a voice id or -1 if the pool is exhausted.

Parameters

sound Object
a handle returned from one of the load methods
volume float
0.0 (silent) to 1.0 (full)
pan float
-1.0 (full left) to 1.0 (full right), 0.0 centered
rate float
playback rate / pitch, 1.0 is normal (typically 0.5 to 2.0)
loop int
0 plays once, -1 loops forever, n repeats n extra times

setVolume

public abstract void setVolume(int voiceId, float volume)

setRate

public abstract void setRate(int voiceId, float rate)

setPan

public abstract void setPan(int voiceId, float pan)

pauseVoice

public abstract void pauseVoice(int voiceId)

resumeVoice

public abstract void resumeVoice(int voiceId)

stopVoice

public abstract void stopVoice(int voiceId)

stopAll

public abstract void stopAll()
Stops every currently playing voice.

autoPause

public abstract void autoPause()
Pauses all active playback (for example when the app is sent to the background).

autoResume

public abstract void autoResume()
Resumes playback paused by #autoPause().

unloadSound

public abstract void unloadSound(Object sound)
Releases a single loaded sound and its buffers.

release

public abstract void release()
Releases the whole pool and all loaded sounds.