# Interface SoundPoolPeer

All Known Subinterfaces:
  `CompletionAwareSoundPoolPeer`

public interface SoundPoolPeer

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.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `autoPause()`
  Pauses all active playback (for example when the app is sent to the background).
  `void`
  `autoResume()`
  Resumes playback paused by `#autoPause()`.
  `Object`
  `loadSound(InputStream data, String mimeType)`
  Loads a short sound from a stream, decoding/buffering it up front so that playback latency is paid here rather than at `#play`.
  `Object`
  `loadSound(String uri)`
  Loads a short sound from a uri (for example a `jar://` resource path).
  `void`
  `pauseVoice(int voiceId)`
  `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.
  `void`
  `release()`
  Releases the whole pool and all loaded sounds.
  `void`
  `resumeVoice(int voiceId)`
  `void`
  `setPan(int voiceId, float pan)`
  `void`
  `setRate(int voiceId, float rate)`
  `void`
  `setVolume(int voiceId, float volume)`
  `void`
  `stopAll()`
  Stops every currently playing voice.
  `void`
  `stopVoice(int voiceId)`
  `void`
  `unloadSound(Object sound)`
  Releases a single loaded sound and its buffers.

- ## Method Details

  - ### loadSound

    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.
    Throws:
      `IOException`

  - ### loadSound

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

  - ### play

    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`: a handle returned from one of the load methods

    - `volume`: 0.0 (silent) to 1.0 (full)

    - `pan`: -1.0 (full left) to 1.0 (full right), 0.0 centered

    - `rate`: playback rate / pitch, 1.0 is normal (typically 0.5 to 2.0)

    - `loop`: 0 plays once, -1 loops forever, n repeats n extra times

  - ### setVolume

    void setVolume(int voiceId, float volume)

  - ### setRate

    void setRate(int voiceId, float rate)

  - ### setPan

    void setPan(int voiceId, float pan)

  - ### pauseVoice

    void pauseVoice(int voiceId)

  - ### resumeVoice

    void resumeVoice(int voiceId)

  - ### stopVoice

    void stopVoice(int voiceId)

  - ### stopAll

    void stopAll()
    Stops every currently playing voice.

  - ### autoPause

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

  - ### autoResume

    void autoResume()
    Resumes playback paused by `#autoPause()`.

  - ### unloadSound

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

  - ### release

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