Skip to content

EZ Web Audio / initAudio

Function: initAudio()

initAudio(useIosMuteWorkaround): Promise<void>

Defined in: index.ts:79

Optionally initialize the audio system explicitly.

The library creates the AudioContext lazily on first use, so calling this function is not required. Use it when you need explicit control over initialization timing (iOS mute workaround, pre-warming the context).

Note: If called explicitly, it must be in response to a user interaction (click, tap, keypress) due to browser autoplay policies.

Parameters

useIosMuteWorkaround

boolean = true

Whether to apply iOS mute switch workaround (default: true)

Returns

Promise<void>

Throws

If AudioContext cannot be created or is interrupted

Example

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

// Optional explicit initialization
button.addEventListener('click', async () => {
  await initAudio() // Optional — for explicit control
  const sound = await createSound('click.mp3')
  sound.play()
})

// Or just use factory functions directly (AudioContext created automatically)
button.addEventListener('click', async () => {
  const sound = await createSound('click.mp3')
  sound.play()
})