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.
loopStartnumber0Time mark (seconds) where looping starts. Only has effect when loop is true and loopEnd is greater than zero.
loopEndnumber0Time mark (seconds) where looping ends. When loop is true and this is greater than zero, the audio loops between loopStart and loopEnd; otherwise the whole track loops.
startAtnumber0Time mark (seconds) where playback starts from when playing from a stopped state.
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.
currentTimenumberThe current playback time mark in seconds (with decimals), updated each frame.

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).