AudioPlayer

View source

Stores the state needed to play an Audio asset from an ECS entity.

Pair it with Timer to control one-shot or repeating playback. The component supplies the asset handle, graph destination, and playback state used to create a browser audio source.

Examples

import { World } from '@wimaengine/ecs'
import { AudioPlayer } from '@wimaengine/audio'
import { Timer, TimerMode } from '@wimaengine/time'

const world = new World()

// Pass a live handle from the world's audio asset resource.
function spawnAudioEntity(audioHandle) {
  return world.spawn([
    new AudioPlayer({ audio: audioHandle }),
    new Timer({ mode: TimerMode.Once })
  ])
}

Constructor


constructor(options?: AudioPlayerOptions)

View source

Creates an audio player with an optional asset handle and graph destination.

Both values are stored as provided. The constructor does not resolve the asset, reserve an AudioGraph slot, or create a Web Audio node.

Parameters

NameDescriptionDefault
options?

Asset handle and optional graph destination.

{}

Properties


sourceNode: number

View source

ID of this player’s source slot in an AudioGraph.

A slot is assigned after the audio handle resolves for a player paired with an Timer. The slot can contain undefined before the first source is created and while the timer is paused or complete. Its paired timer takes its duration from the decoded buffer. Once uses a non-looping source, while Repeat uses a looping one, and the timer’s speed controls the source’s playback rate. Pausing or completing the timer clears the current source. This ID is graph state, not an AudioBufferSourceNode.

attach: number

View source

Optional AudioGraph node to which the source slot is connected.

When a source slot is first assigned, it connects to this destination. If it is omitted, the slot connects to the graph root, which is the audio destination. Changing attach after that does not rewire the existing graph edge.

audio: Handle<Audio>

View source

Live handle for the Audio asset to play.

The handle identifies an entry in the world’s AudioAssets pool. An absent or stale handle leaves the player without a source until a valid handle is available. When the component is removed through AudioPlugin, its removal hook drops this handle.

Methods


staticcopy(source: AudioPlayer, target?: AudioPlayer): AudioPlayer

View source

Copies an audio player’s fields into a target component.

sourceNode, attach, and audio are assigned directly. The audio handle is not passed to Handle#clone, so source and target share the same handle object and no additional asset reference is acquired.

Parameters

NameDescriptionDefault
source

Player to copy.

—
target?

Component to populate.

...

Returns

The populated target.

staticclone(target: AudioPlayer): AudioPlayer

View source

Creates a player by applying AudioPlayer.copy.

The returned player shares the source’s audio handle and copies its runtime graph ID. It does not acquire a separate handle reference or create a new graph slot.

Parameters

NameDescriptionDefault
target

Player to clone.

—

Returns

A player with copied runtime fields.

statictoSnapshot(player: AudioPlayer, world: World): AudioPlayerSnapshot

View source

Creates a persistence snapshot of this player.

The snapshot stores sourceNode and attach, plus a HandleSnapshot for audio when a handle is present. It stores asset locator data, not the loaded Audio object or live Web Audio node. Restoring it with AudioPlayer.fromSnapshot restores the recorded IDs but does not rebuild the graph slot.

Parameters

NameDescriptionDefault
player

Live player being snapshotted.

—
world

World used to resolve asset metadata.

—

Returns

Snapshot of the player’s persisted fields.