Skip to content
  • Android
  • iOS
  • Web

Microphone

Request microphone access and record audio under user control.

Examples

Quick start

Bind recording to explicit controls and release any returned sound with audio.release when done. The browser requires a secure context.

nayan-ui/microphone
import { microphone } from 'nayan-ui/microphone';
import { warmUpEngine } from 'nayan-ui/games';

export async function startRecording() {
  await warmUpEngine();
  if (!microphone.supported || !(await microphone.requestPermission())) return false;
  return microphone.start({ maxSeconds: 10 });
}

export function finishRecording() {
  const take = microphone.take(10);
  microphone.stop();
  return take;
}

Play back a recorded take and release its audio

Call after starting an authorized recording. Call dispose after playback or when leaving the screen.

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

export function previewTake(seconds = 5) {
  const take = microphone.take(seconds, { levelsPerSecond: 20, skipEnd: 0.1 });
  microphone.stop();
  if (!take) return null;
  const voice = audio.play(take.sound);
  return {
    duration: take.duration,
    waveform: take.levels,
    dispose() { audio.stop(voice); audio.release(take.sound); }
  };
}

Tags