Skip to content
  • Android
  • iOS
  • Web

Audio

Load sounds and play them with volume, playback rate and spatial controls.

Examples

Quick start

On the web, start playback in response to a user gesture.

nayan-ui/audio
import { audio } from 'nayan-ui/audio';

export async function playSound(uri: string) {
  const sound = await audio.loadSound(uri);
  const voice = audio.play(sound, { volume: 0.6 });
  return () => {
    audio.stop(voice);
    audio.release(sound);
  };
}

Loop, seek, and mix a sound

nayan-ui/audio
import { audio } from 'nayan-ui/audio';

export async function openLoop(uri: string) {
  const sound = await audio.loadSound(uri);
  const voice = audio.play(sound, { loop: true, volume: 0.4 });
  return {
    pause: () => voice !== null && audio.pause(voice),
    resume: () => voice !== null && audio.resume(voice),
    seek: (seconds: number) => voice !== null && audio.seek(voice, seconds),
    mix: (volume: number, pan: number) => voice !== null && audio.setVoiceMix(voice, volume, pan),
    dispose() { audio.stop(voice); audio.release(sound); }
  };
}

Use a named sound bank

nayan-ui/audio
import { SoundBank } from 'nayan-ui/audio';

export const effects = new SoundBank({
  click: '/sounds/click.wav',
  complete: '/sounds/complete.wav'
});

export async function previewEffects() {
  await effects.ready;
  return effects.play('complete', { volume: 0.5, pitch: 1.1 });
}

Decode WAV bytes and inspect audio levels

nayan-ui/audio
import { audio } from 'nayan-ui/audio';

export async function inspectWav(bytes: ArrayBuffer) {
  const sound = await audio.decodeWav(bytes);
  try {
    return { duration: audio.duration(sound), levels: audio.levels(sound, 20) };
  } finally {
    audio.release(sound);
  }
}
Related guide

Tags