Skip to content

EZ Web Audio / createSound

Function: createSound()

Call Signature

createSound(input): Promise<Sound<BaseSoundEventMap>>

Defined in: packages/core/src/index.ts:435

Create a Sound from an audio file URL, ArrayBuffer, Blob, or File.

Sound is for one-shot audio playback (sound effects, UI sounds). Each call to .play() creates a new audio source, allowing overlapping playback. Use createTrack instead for music with pause/resume/seek.

Parameters

input

AudioInput

URL string, ArrayBuffer, Blob, or File containing audio data

Returns

Promise<Sound<BaseSoundEventMap>>

Promise resolving to a Sound instance

Throws

If the audio file cannot be loaded or decoded

Example

typescript
import { createSound } from 'ez-web-audio'

// From URL
const click = await createSound('click.mp3')
click.play()

// From ArrayBuffer (e.g., from fetch or File API)
const response = await fetch('click.mp3')
const buffer = await response.arrayBuffer()
const click2 = await createSound(buffer)

// From File (e.g., drag-and-drop)
input.addEventListener('change', async (e) => {
  const file = e.target.files[0]
  const sound = await createSound(file)
  sound.play()
})

Call Signature

createSound(audioContext, input): Promise<Sound<BaseSoundEventMap>>

Defined in: packages/core/src/index.ts:436

Create a Sound from an audio file URL, ArrayBuffer, Blob, or File.

Sound is for one-shot audio playback (sound effects, UI sounds). Each call to .play() creates a new audio source, allowing overlapping playback. Use createTrack instead for music with pause/resume/seek.

Parameters

audioContext

BaseAudioContext

input

AudioInput

URL string, ArrayBuffer, Blob, or File containing audio data

Returns

Promise<Sound<BaseSoundEventMap>>

Promise resolving to a Sound instance

Throws

If the audio file cannot be loaded or decoded

Example

typescript
import { createSound } from 'ez-web-audio'

// From URL
const click = await createSound('click.mp3')
click.play()

// From ArrayBuffer (e.g., from fetch or File API)
const response = await fetch('click.mp3')
const buffer = await response.arrayBuffer()
const click2 = await createSound(buffer)

// From File (e.g., drag-and-drop)
input.addEventListener('change', async (e) => {
  const file = e.target.files[0]
  const sound = await createSound(file)
  sound.play()
})