Table of Contents

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

useSynchronizationContext bool

When 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. When false, events are raised synchronously on the Windows audio worker thread and handlers must be nonblocking.

Returns

MMDeviceNotificationClient

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

disposing bool

true when called from Dispose().

EnumerateAudioEndPoints(DataFlow, DeviceState)

Returns every audio endpoint that matches the supplied direction and state mask.

public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState dwStateMask)

Parameters

dataFlow DataFlow

Render, capture, or both.

dwStateMask DeviceState

Bitmask of DeviceState values to include.

Returns

MMDeviceCollection

GetDefaultAudioEndpoint(DataFlow, Role)

Returns the default endpoint for the given direction and role.

public MMDevice GetDefaultAudioEndpoint(DataFlow dataFlow, Role role)

Parameters

dataFlow DataFlow
role Role

Returns

MMDevice

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

id string

Returns

MMDevice

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

dataFlow DataFlow
role Role

Returns

bool

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

dataFlow DataFlow

Render or capture.

role Role

Console, multimedia, or communications role.

device MMDevice

The default device on success; null otherwise.

Returns

bool