Overview
The Swift SDK abstracts device management intoDeviceManager.shared, so you don’t have to piece together platform differences from scratch for device enumeration, hot-plug monitoring, and audio session interruption handling.
Common needs include:
- Enumerating cameras, microphones, and speakers
- Letting the user switch devices manually
- Monitoring device plug and unplug events
- Recovering after an interruption on iOS such as an incoming call or Siri
Enumerate devices
Enumerate cameras
macOS: enumerate microphones and speakers
iOS: enumerate audio input ports
AVAudioSession (built-in microphone, Bluetooth, wired headset, and so on).
iOS doesn’t have the independent output device model of desktop systems, so this list doesn’t include the speaker. “Whether sound comes out of the speaker or the receiver” is a separate mechanism—see Audio routing.
Generic query
If you want to build a single device-picker UI layer, you can also use:Switch cameras
For an existingLocalCameraTrack, you can switch to a specific device directly:
LocalCameraTrack.
Switch microphones or audio routes
macOS
iOS
- macOS: switches the input device
- iOS: switches the input port (
AVAudioSession.setPreferredInput);deviceIdcomes fromaudioInputs()
Switch speakers on macOS
On macOS, output device switching goes throughDeviceManager:
LocalMicTrack; it’s bound to the output path of the underlying audio module.
Monitor device changes
Register a listener throughDeviceManagerDelegate:
- In the callback, only refresh the local device list state
- Have the UI layer update its dropdown or picker automatically based on the new device list
Automatic recovery built into the SDK
The Swift SDK already handles several common recovery cases internally:- When the camera in use is unplugged, it tries to switch to an available camera
- On macOS, when the microphone in use is unplugged, it tries to switch back to the default input
- On iOS, after the audio session is interrupted by an incoming call or similar, it recovers when conditions allow (including waiting for the system call to end and retrying on failure—see Audio routing)
- On iOS, camera capture can recover when the app returns to the foreground after going to the background
- Show error messages to the user
- Refresh the UI after the device list changes
- Don’t cache stale
deviceIdvalues
Recommended approach in your app
Recommended approach:- Call
refreshDeviceList()when the page initializes - Save the currently selected
deviceId - After a device change, if the original
deviceIdno longer exists, fall back to the default device automatically