AudioPlayer

The AudioPlayer component plays, pauses, and stops audio sources. It controls volume and looping, and can optionally sync playback speed with the game’s time scale.

Options

OptionTypeDefaultDescription
audioSourceAudioSource | stringThe audio to play: an AudioSource (from AssetManager.getAudio) or an asset URL/name string.
volumenumber1Playback volume.
loopbooleanfalseWhether the audio repeats.
stopOnSceneTransitionbooleantrueWhether the audio stops when a new scene loads.
fixedToTimeScalebooleanfalseWhether the playback rate follows the time scale.

Read-only properties

PropertyTypeDescription
state"stopped" | "playing" | "paused"The current playback state.
playingbooleantrue while playing.
pausedbooleantrue while paused.
stoppedbooleantrue while stopped.

Methods

MethodDescription
play(audioSource?)Plays the audio. An optional source overrides audioSource.
pause()Pauses playback.
stop()Stops playback.

Example

import { AudioPlayer } from "angry-pixel";

this.entityManager.createEntity([
    new AudioPlayer({ audioSource: "music.ogg", volume: 0.5, loop: true }),
]);

The audio file is loaded through the asset manager, typically in the scene’s loadAssets method.

Note: Browsers block audio from starting until the user interacts with the page. The component honors this autoplay policy: if playback is requested before any user interaction, it waits and starts automatically on the first user input (a click, key press, or touch).