audio family
Music playback ownership with an injected backend.
Import these from gamekit/src/audio (a few are also re-exported by the package root, as noted on each).
2 symbols, 2 of them also in the package root.
| Symbol | Summary |
|---|---|
IMusicBackend | interface. Small boundary around music playback so a game may adapt core Audio and tests may use a deterministic fake. |
MusicDirector | class. Owns the current music request, avoiding duplicate changes and restoring temporarily stacked tracks. |
IMusicBackend
ts
import { IMusicBackend } from "gamekit";
interface IMusicBackendSmall boundary around music playback so a game may adapt core Audio and tests may use a deterministic fake.
Members
ts
loadMusic(trackId: string): Promise<any>Loads one track and returns its opaque core audio handle.
trackId: Project-relative track identifier accepted by the core audio backend.- Returns: A promise for the loaded handle.
ts
playMusicHandle(handle: any, volume: float, loop: boolean): voidStarts a loaded track without a transition.
handle: Opaque handle returned byloadMusic.volume: Target gain, where 1 is normal volume.loop: Whether playback repeats after completion.
ts
crossfadeTo(handle: any, durationMs: int, volume: float, loop: boolean): voidTransitions from the active track to a loaded track.
handle: Opaque handle returned byloadMusic.durationMs: Transition duration in milliseconds.volume: Target gain, where 1 is normal volume.loop: Whether playback repeats after completion.
ts
unloadMusic(handle: any): voidCancels and releases a loaded track that is no longer current.
handle: Opaque handle returned byloadMusic.
MusicDirector
ts
import { MusicDirector } from "gamekit";
class MusicDirectorOwns the current music request, avoiding duplicate changes and restoring temporarily stacked tracks. Supply an IMusicBackend implementation or a small adapter that forwards to core Audio; the checker does not currently accept typeof Audio directly as the interface. An outgoing handle remains retained for the full following transition and releases only on the next transition or dispose, so the mixer never fades from a freed track.
Members
ts
constructor(backend: IMusicBackend, crossfadeMs: int = 500)Creates a director over backend with a fixed crossfade length.
backend: Music boundary, normally an adapter over coreAudioor a test fake.crossfadeMs: Crossfade length in milliseconds.- Default: 500
ts
async play(trackId: string): Promise<void>Makes trackId current. Repeating the current id makes no backend request. A replaced handle remains loaded throughout this transition and releases when a later transition begins or this director is disposed.
trackId: Project-relative track identifier.- Returns: A promise that resolves after the requested track is loaded and started or crossfaded.
ts
async push(trackId: string): Promise<void>Temporarily makes trackId current and remembers the prior track for pop.
trackId: Project-relative track identifier.- Returns: A promise that resolves after the requested track is loaded and started or crossfaded.
ts
async pop(): Promise<void>Brings back the most recently pushed track; does nothing when the temporary-track stack is empty.
- Returns: A promise that resolves after the prior track is loaded and crossfaded.
ts
dispose(): voidReleases the current handle and the one retained for a completed fade, then clears all track requests. Call this when the director's owner is finished with music playback.