Skip to content

1681-engine / index / MusicPlayer

Class: MusicPlayer ​

Defined in: src/audio/MusicPlayer.ts:38

Background music in layers: each named channel ('music' by default, 'ambience', any other) plays one looping track at a time, crossfaded when it changes, all at engineSettings.musicVolume. Tracks stream through <audio> elements (no decoding a whole song into memory).

Browsers start sound only after a user gesture: a track played before one starts on the first tap or key. On iOS the graph's context is woken inside that gesture too, and again after the app comes back from the background.

ts
music.play('music/forest.ogg');                                     // the 'music' channel
music.channel('ambience').play('amb/wind.ogg', { volume: 0.5 });     // a second layer under it
music.play('music/boss.ogg', { fadeMs: 600 });                      // crossfades the first only
music.stop(2000);

Constructors ​

Constructor ​

ts
new MusicPlayer(): MusicPlayer;

Returns ​

MusicPlayer

Accessors ​

current ​

Get Signature ​

ts
get current(): string | null;

Defined in: src/audio/MusicPlayer.ts:65

The url the 'music' channel plays, if any.

Returns ​

string | null

Methods ​

channel() ​

ts
channel(name?): MusicChannel;

Defined in: src/audio/MusicPlayer.ts:54

The channel named name, made on first use.

Parameters ​

ParameterTypeDefault value
namestring'music'

Returns ​

MusicChannel


configure() ​

ts
configure(options): this;

Defined in: src/audio/MusicPlayer.ts:47

Picks the route and background behaviour; call before the first track for the route to apply to every track.

Parameters ​

ParameterType
optionsMusicOptions

Returns ​

this


destroy() ​

ts
destroy(): void;

Defined in: src/audio/MusicPlayer.ts:87

Returns ​

void


pause() ​

ts
pause(): void;

Defined in: src/audio/MusicPlayer.ts:79

Returns ​

void


play() ​

ts
play(url, options?): void;

Defined in: src/audio/MusicPlayer.ts:70

Plays url on the 'music' channel.

Parameters ​

ParameterType
urlstring
options?MusicPlayOptions

Returns ​

void


resume() ​

ts
resume(): void;

Defined in: src/audio/MusicPlayer.ts:83

Returns ​

void


stop() ​

ts
stop(fadeMs?): void;

Defined in: src/audio/MusicPlayer.ts:75

Fades every channel out.

Parameters ​

ParameterTypeDefault value
fadeMsnumber1200

Returns ​

void

Released under the MIT License.