@hyperfrontend/ui-utils/audioaudio
Audio-context initialization helper for browser audio.
setupAudio resolves a single shared AudioContext instance, lazily creating one on first call and reusing it on subsequent calls so applications don't end up with multiple contexts competing for the audio device. Useful as the entry point in any browser code that needs to play sounds, decode audio buffers, or schedule audio nodes.
API Reference
ƒ Functions
Sets up an AudioContext by waiting for user interaction on a specified element. Required for browsers that block audio without user gesture.
Parameters
| Name | Type | Description |
|---|---|---|
§selector | ElementRefOrString | Either an HTMLElement reference or a CSS selector string for the target element |
Returns
Promise<AudioContext>A promise that resolves to an AudioContext once the user interacts with the element
Example
Setting up AudioContext on user interaction
const playButton = document.getElementById('play-button')
const audioContext = await setupAudio(playButton)
// Audio context is now ready after user clicked the button
const oscillator = audioContext.createOscillator()
oscillator.connect(audioContext.destination)
oscillator.start()