This project has moved. For the latest updates, please go here.

Adding RegisterEndpointNotificationCallback & UnRegisterEndpointNotificationCallback methods to MMDeviceEnumerator Class

Feb 12, 2014 at 8:03 AM
Hi Guys,

We are using Naudio to enumerate through connected audio devices. We had this requirement to detect device connection and removal. I knew NAudio provided IMMNotificationClient interface. But there is very little documentation available. Found we can make use to IMMNotificationClient only through an object of IMMDeviceEnumerator only to find RegisterEndpointNotificationCallback & UnRegisterEndpointNotificationCallback are not in implemented in the below mentioned class
public class MMDeviceEnumerator
    {
        private IMMDeviceEnumerator _realEnumerator;

        /// <summary>
        /// Enumerate Audio Endpoints
        /// </summary>
        /// <param name="dataFlow">Desired DataFlow</param>
        /// <param name="dwStateMask">State Mask</param>
        /// <returns>Device Collection</returns>
        public MMDeviceCollection EnumerateAudioEndPoints(DataFlow dataFlow, DeviceState dwStateMask)
        {
            IMMDeviceCollection result;
            Marshal.ThrowExceptionForHR(_realEnumerator.EnumAudioEndpoints(dataFlow, dwStateMask, out result));
            return new MMDeviceCollection(result);
        }

        /// <summary>
        /// Get Default Endpoint
        /// </summary>
        /// <param name="dataFlow">Data Flow</param>
        /// <param name="role">Role</param>
        /// <returns>Device</returns>
        public MMDevice GetDefaultAudioEndpoint(DataFlow dataFlow, Role role)
        {
            IMMDevice _Device = null;
            Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)_realEnumerator).GetDefaultAudioEndpoint(dataFlow, role, out _Device));
            return new MMDevice(_Device);
        }

        /// <summary>
        /// Get device by ID
        /// </summary>
        /// <param name="ID">Device ID</param>
        /// <returns>Device</returns>
        public MMDevice GetDevice(string ID)
        {
            IMMDevice _Device = null;
            Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)_realEnumerator).GetDevice(ID, out _Device));
            return new MMDevice(_Device);
        }

        /// <summary>
        /// Creates a new MM Device Enumerator
        /// </summary>
        public MMDeviceEnumerator()
        {
#if !NETFX_CORE
            if (System.Environment.OSVersion.Version.Major < 6)
            {
                throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
            }
#endif
            _realEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
        }
    }
I request you to add these implementations.

I added these implementations for our project as below
/// <summary>
       /// Registers a call back for Device Events
       /// </summary>
        /// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface</param>
       /// <returns></returns>
        public int RegisterEndpointNotificationCallback([In] [MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
        {
            return _realEnumerator.RegisterEndpointNotificationCallback(client);

        }

        /// <summary>
        /// UnRegisters a call back for Device Events
        /// </summary>
        /// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface </param>
        /// <returns></returns>
        public int UnRegisterEndpointNotificationCallback([In] [MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
        {
            return _realEnumerator.UnregisterEndpointNotificationCallback(client);

        }
Please correct me if there is an error in my code.

Thanks.