Manages audio files and players, providing control over playback on three audio channels.

The AudioManager handles audio files and players, offering control over playback on three distinct channels.

AudioChannel

The AudioManager is able to play audio with spatial positioning. Keep in mind that for this to work correctly, you will need to set up the audio-listener component!

enum Sounds {
Click,
GunShot,
}

// AudioManager can't be constructed in a non-browser environment!
export const am = window.AudioContext ? new AudioManager() : null;

if (am != null) {
am.load('path/to/click.wav', Sounds.Click);
am.load('path/to/gunshot.wav', Sounds.GunShot);
}

onPress() {
am.play(Sounds.Click, {volume: 0.8, position: [0, 5, 1]});
}
interface IAudioManager {
    get amountOfFreePlayers(): number;
    autoplay(id: number, config?: PlayConfig): number;
    getSourceIdFromPlayId(playId: number): number;
    load(path: string | string[], id: number): void;
    loadBatch(...pair: [string | string[], number][]): void;
    pause(playId: number): void;
    pauseAll(): void;
    play(id: number, config?: PlayConfig): number;
    playOneShot(id: number, config?: PlayConfig): void;
    remove(id: number): void;
    removeAll(): void;
    resume(playId: number): void;
    resumeAll(): void;
    setGlobalVolume(channel: AudioChannel, volume: number, time: number): void;
    stop(playId: number): void;
    stopAll(): void;
    stopOneShots(): void;
}

Implemented by

Accessors

  • get amountOfFreePlayers(): number
  • Gets the current amount of free players in the audio manager.

    Returns number

    Use this to check how many resources your current project is using.

Methods

  • Same as play() but waits until the user has interacted with the website.

    Parameters

    • id: number

      ID of the file that should be played.

    • Optionalconfig: PlayConfig

      Optional parameter that will configure how the audio is played. Is no configuration provided, the audio will play at volume 1.0, without panning and on the SFX channel, priority set to false.

    Returns number

    The playId that identifies this specific playback, so it can be stopped or identified in the emitter.

  • Gets the sourceId of a playId.

    Parameters

    • playId: number

      of which to get the sourceId from.

    Returns number

  • Decodes and stores the given audio files and associates them with the given ID.

    Parameters

    • path: string | string[]

      Path to the audio files. Can either be a single string or a list of strings.

    • id: number

      Identifier for the given audio files.

    Returns void

    A Promise that resolves when all files are successfully loaded.

    Is there more than one audio file available per id, on playback, they will be selected at random. This enables easy variation of the same sounds!

  • Same as load(), but lets you easily load a bunch of files without needing to call the manager everytime.

    Parameters

    • Rest...pair: [string | string[], number][]

      Pair of source files and associating identifier. Multiple pairs can be provided as separate arguments.

    Returns void

    A Promise that resolves when all files are successfully loaded.

    load

  • Pauses a playing audio.

    Parameters

    • playId: number

      Id of the source that should be paused.

    Returns void

  • Pauses all playing players.

    Returns void

  • Plays the audio file associated with the given ID.

    Parameters

    • id: number

      ID of the file that should be played.

    • Optionalconfig: PlayConfig

      Optional parameter that will configure how the audio is played. Is no configuration provided, the audio will play at volume 1.0, without panning and on the SFX channel, priority set to false.

    Returns number

    The playId that identifies this specific playback, so it can be stopped or identified in the emitter. If playback could not be started, an invalid playId is returned.

    If the 'priority' parameter is set to true, the audio playback will not be interrupted to allocate a player in case all players are currently occupied. If 'priority' is set to false (default), playback may be interrupted to allocate a player for a new 'play()' call.

  • Plays the audio file associated with the given ID until it naturally ends.

    Parameters

    • id: number

      ID of the file that should be played.

    • Optionalconfig: PlayConfig

      Optional parameter that will configure how the audio is played. Note that only the position and volume settings will affect the playback.

    Returns void

    • IDs can be triggered as often as there are one-shot players in the AudioManager.
    • One shots work with First-In-First-Out principle. If all players are occupied, the manager will stop the one that started playing first, to free up a player for the new ID.
    • One-shots are always connect to the SFX channel.
    • One-shots cant loop.
    • One-shots can only be stopped all at once with stopOneShots().
    • One-shots can't be assigned a priority.

    since > 1.2.0, use play() instead.

  • Removes all decoded audio from the manager that is associated with the given ID.

    Parameters

    • id: number

      Identifier of the audio that should be removed.

    Returns void

    This will stop playback of the given ID.

  • Removes all decoded audio from the manager, effectively resetting it.

    Returns void

    This will stop playback entirely.

  • Resumes a paused audio.

    Parameters

    • playId: number

      Id of the source that should be resumed.

    Returns void

  • Resumes all paused players.

    Returns void

  • Sets the volume of the given audio channel.

    Parameters

    • channel: AudioChannel

      Specifies the audio channel that should be modified.

    • volume: number

      Volume that the channel should be set to.

    • time: number

      Optional time parameter that specifies the time it takes for the channel to reach the specified volume in seconds (Default is 0).

    Returns void

  • Stops the audio associated with the given ID.

    Parameters

    • playId: number

      Specifies the exact audio that should be stopped.

    Returns void

    Obtain the playId from the play() method.

    play

  • Stops all audio.

    Returns void

  • Stops playback of all one-shot players.

    Returns void

    since >1.2.0, use regular play() with stop() instead.