Mic Selector

PreviousNext

Microphone input selector with device management.

Installation

pnpm dlx @elevenlabs/agents-cli@latest components add mic-selector

Usage

import { MicSelector } from "@/components/ui/mic-selector"

Basic Usage

<MicSelector />

Controlled

const [selectedDevice, setSelectedDevice] = useState("")
 
<MicSelector value={selectedDevice} onValueChange={setSelectedDevice} />

With Mute Control

const [selectedDevice, setSelectedDevice] = useState("")
const [isMuted, setIsMuted] = useState(false)
 
<MicSelector
  value={selectedDevice}
  onValueChange={setSelectedDevice}
  muted={isMuted}
  onMutedChange={setIsMuted}
/>

Custom Styling

<MicSelector className="w-full max-w-md" />

Using the Hook

import { useAudioDevices } from "@/components/ui/mic-selector"
 
const { devices, loading, error, hasPermission, loadDevices } =
  useAudioDevices()
 
// Access available microphones
devices.map((device) => console.log(device.label, device.deviceId))

API Reference

MicSelector

A dropdown selector for choosing audio input devices with live waveform preview.

Props

PropTypeDescription
valuestringSelected device ID (controlled)
onValueChange(deviceId: string) => voidCallback when device selection changes
mutedbooleanMute state (controlled)
onMutedChange(muted: boolean) => voidCallback when mute state changes
disabledbooleanDisables the selector dropdown
classNamestringOptional CSS classes for the container

useAudioDevices

A hook for managing audio input devices.

Returns

PropertyTypeDescription
devicesAudioDevice[]Array of available audio input devices
loadingbooleanWhether devices are being loaded
errorstring | nullError message if device loading failed
hasPermissionbooleanWhether microphone permission was granted
loadDevices() => Promise<void>Function to request permission and reload

AudioDevice Type

interface AudioDevice {
  deviceId: string
  label: string
  groupId: string
}

Features

  • Device Management: Automatically detects and lists available microphones
  • Live Preview: Real-time audio waveform visualization when dropdown is open
  • Mute Toggle: Control preview audio on/off with controlled or uncontrolled state
  • Permission Handling: Gracefully handles microphone permissions
  • Auto-selection: Automatically selects first available device
  • Device Changes: Listens for device connection/disconnection events
  • Clean Labels: Automatically removes device metadata from labels
  • Flexible Control: Works in both controlled and uncontrolled modes for device selection and mute state

Notes

  • Uses the LiveWaveform component for audio visualization
  • Automatically requests microphone permissions when opening dropdown
  • Preview shows scrolling waveform of microphone input
  • Device list updates automatically when devices are connected/disconnected
  • Works in both controlled and uncontrolled modes for device selection and mute state
  • Mute state can be controlled from parent component for integration with recording controls
  • Can be disabled during active recording or other operations
  • Cleans up audio streams properly on unmount