Creates a new AudioManager instance. The AudioContext is created lazily on first use or via initAudio.
ReadonlyonObservable that emits when a track starts playing.
ReadonlyonObservable that emits when a track finishes playing.
ReadonlyonObservable that emits when a track's metadata is loaded and ready to play.
ReadonlyonObservable that emits when an audio error occurs.
Initializes the AudioContext and resumes it if suspended. Call this method on user interaction to ensure audio playback works in browsers.
A promise that resolves when the AudioContext is ready.
Resumes a suspended AudioContext. Browsers require user interaction before allowing audio playback. Call this method in response to a user gesture (click, touch, keypress).
A promise that resolves when the AudioContext is resumed.
Suspends the AudioContext to save resources when audio is not needed. Useful for pausing all audio when the application loses focus or enters background.
A promise that resolves when the AudioContext is suspended.
Enables or disables audio playback for a specific group. When disabled, all sounds in the group are muted and continuous playback is paused.
The audio group name (e.g., 'sfx', 'music', 'ambient').
Whether the group should be enabled.
Sets the volume for a specific audio group. The value is clamped to the range [0, 1].
The audio group name (e.g., 'sfx', 'music', 'ambient').
The volume level (0 = silent, 1 = full volume).
Sets the maximum number of concurrent sounds for a group. When the limit is reached, new sounds in the group will not play.
The audio group name.
Maximum number of simultaneous sounds (minimum: 1).
Checks if all tracks for a key are fully loaded and ready to play.
The audio key to check.
true if all tracks for the key are loaded, false otherwise.
Gets detailed loading status for a specific audio key.
The audio key to check.
An object with total tracks, loaded count, and ready status.
Preloads audio tracks into memory for low-latency playback. This is especially important for one-shot sounds (SFX) that need instant playback.
Array of audio keys to preload.
A promise that resolves when all tracks are loaded.
Registers an audio track with the manager. Multiple tracks can be registered under the same key for random variation.
Unique identifier for the sound (e.g., 'explosion', 'footstep').
Audio group for volume/mute control (e.g., 'sfx', 'music').
URL or path to the audio file.
// Add a single sound effect
audio.addAudioTrack('explosion', 'sfx', '/sounds/explosion.wav');
// Add multiple variations for the same key (random selection on play)
audio.addAudioTrack('footstep', 'sfx', '/sounds/footstep1.wav');
audio.addAudioTrack('footstep', 'sfx', '/sounds/footstep2.wav');
audio.addAudioTrack('footstep', 'sfx', '/sounds/footstep3.wav');
// Add background music
audio.addAudioTrack('battle', 'music', '/music/battle-theme.mp3');
Plays a one-shot sound effect. If multiple tracks are registered under the key, one is selected randomly. The sound plays to completion and cannot be stopped or paused.
The audio key to play.
Starts continuous playback on a channel. When a track ends, the next track is automatically played (looping through all tracks). Ideal for background music, ambient sounds, or any audio that should loop.
The audio key to play.
Optional channel identifier (default: 'default'). Use different channels for simultaneous playback (e.g., music + ambient).
// Play background music on default channel
audio.playContinuous('battle-music');
// Play ambient sounds on a separate channel
audio.playContinuous('forest-ambient', 'ambient');
// Both play simultaneously and can be controlled independently
audio.pauseContinuous('ambient'); // Pause only ambient
audio.stopContinuous(); // Stop only default channel
Pauses continuous playback on a channel. The playback position is preserved and can be resumed with resumeContinuous.
The channel to pause (default: 'default').
Starts playback with a gradual volume fade-in effect. Uses the Web Audio API's native gain automation for smooth transitions.
The audio key to play.
Fade duration in milliseconds.
The channel to use (default: 'default').
A promise that resolves when the fade-in completes.
Gradually fades out and stops the current playback on a channel. Uses the Web Audio API's native gain automation for smooth transitions.
Fade duration in milliseconds.
The channel to fade out (default: 'default').
A promise that resolves when the fade-out completes and playback stops.
Cross-fades from the current track to a new track on the same channel. The old track fades out while the new track fades in simultaneously.
The audio key of the new track to play.
Cross-fade duration in milliseconds.
The channel to cross-fade on (default: 'default').
A promise that resolves when the cross-fade completes.
Gets information about all active continuous playback channels.
An object mapping channel IDs to their playback info.
Gets information about a specific playback channel.
The channel to query (default: 'default').
Channel info if active, or null if no playback on channel.
Sets the playback speed/rate for a channel. The value is clamped to the range [0.25, 4.0].
Playback rate (1.0 = normal, 0.5 = half speed, 2.0 = double speed).
The channel to modify (default: 'default').
Gets the current playback rate for a channel.
The channel to query (default: 'default').
The playback rate, or null if no playback on channel.
Gets the current playback time in seconds.
The channel to query (default: 'default').
Current time in seconds, or null if no playback on channel.
Gets the total duration of the current track in seconds.
The channel to query (default: 'default').
Duration in seconds, or null if unavailable.
Gets comprehensive playback information for a channel.
The channel to query (default: 'default').
Playback info including time, duration, rate, and volume.
Stops all active one-shot sounds in a specific group. This immediately stops and cleans up all playing sounds that belong to the specified group.
The group key to stop all sounds for (e.g., 'sfx', 'ui', 'ambient')
Destroys the AudioManager and releases all resources. Stops all playback, disconnects all audio nodes, and closes the AudioContext. After calling destroy(), the AudioManager instance should not be used.
AudioManager is a framework-agnostic audio management system built on the Web Audio API. It provides centralized control for organizing, playing, and managing audio assets in games and interactive applications.
Example