Skip to main content

Function: createAudio()

createAudio(src, options?): AudioTrack

Defined in: fabric.ts:35

Factory function to create a new audio track.

Follows the project's factory-first pattern similar to createEntity() and newStory(). Creates an AudioTrack instance with reactive state management and automatic registration with the AudioManager.

Parameters

src

string

Path to the audio file (relative or absolute URL)

options?

AudioOptions

Optional audio configuration

Returns

AudioTrack

A reactive AudioTrack instance

Example

// Create background music with auto-save
const bgMusic = createAudio('assets/music/theme.mp3', {
id: 'bg-music',
loop: true,
volume: 0.5,
});

await bgMusic.play();
bgMusic.setVolume(0.7);

// Create sound effect (no ID = no auto-save)
const clickSound = createAudio('assets/sfx/click.mp3', {
volume: 0.8,
});

clickSound.play();