Class MMDeviceEnumerator
- Namespace
- NAudio.CoreAudioApi
- Assembly
- NAudio.Wasapi.dll
Entry point to the Windows Core Audio device enumeration API
(IMMDeviceEnumerator). Use this to discover audio endpoints,
resolve the default render or capture device, look up a device by ID,
and subscribe to endpoint change notifications.
public class MMDeviceEnumerator : IDisposable
- Inheritance
-
MMDeviceEnumerator
- Implements
- Inherited Members
Constructors
MMDeviceEnumerator()
Activates the system MMDeviceEnumerator COM object.
public MMDeviceEnumerator()
Methods
CreateNotificationClient(bool)
Creates an event-based subscription to endpoint change notifications — subscribe to the events on the returned MMDeviceNotificationClient and dispose it when done. This is the supported way to observe device add/remove, state changes, default-device changes, and property changes; the underlying COM callback interface is handled internally.
public MMDeviceNotificationClient CreateNotificationClient(bool useSynchronizationContext = true)
Parameters
useSynchronizationContextboolWhen
true(the default), events are marshalled to the SynchronizationContext captured on the calling thread (e.g. the UI thread), so handlers can update UI directly. Whenfalse, events are raised synchronously on the Windows audio worker thread and handlers must be nonblocking.
Returns
Remarks
The returned client keeps this enumerator alive for its lifetime; disposing either the client or this enumerator ends the subscription.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Releases the underlying COM enumerator. Safe to call multiple times.
protected virtual void Dispose(bool disposing)
Parameters
EnumerateAudioEndPoints(DataFlow, DeviceState)
Returns every audio endpoint that matches the supplied direction and state mask.
public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState dwStateMask)
Parameters
dataFlowDataFlowRender, capture, or both.
dwStateMaskDeviceStateBitmask of DeviceState values to include.
Returns
GetDefaultAudioEndpoint(DataFlow, Role)
Returns the default endpoint for the given direction and role.
public MMDevice GetDefaultAudioEndpoint(DataFlow dataFlow, Role role)
Parameters
Returns
Exceptions
- CoreAudioException
Thrown if no default endpoint exists. Use HasDefaultAudioEndpoint(DataFlow, Role) or TryGetDefaultAudioEndpoint(DataFlow, Role, out MMDevice) for the non-throwing variants.
GetDevice(string)
Resolves an endpoint by its ID string.
public MMDevice GetDevice(string id)
Parameters
idstring
Returns
HasDefaultAudioEndpoint(DataFlow, Role)
Returns true if a default endpoint exists for the given direction and role,
without allocating a wrapper or throwing on the common "no default" case.
public bool HasDefaultAudioEndpoint(DataFlow dataFlow, Role role)
Parameters
Returns
TryGetDefaultAudioEndpoint(DataFlow, Role, out MMDevice)
Attempts to resolve the default endpoint for the given direction and role.
Returns false when no default exists, without throwing.
public bool TryGetDefaultAudioEndpoint(DataFlow dataFlow, Role role, out MMDevice device)
Parameters
dataFlowDataFlowRender or capture.
roleRoleConsole, multimedia, or communications role.
deviceMMDeviceThe default device on success;
nullotherwise.