locked
Detect audio device plugged unplugged RRS feed

  • Question

  • Hi

    I am trying to get an event when an audio device is plugged or unplugged

    I was trying to use CoreAudio API I created ImmNotificationClient.cs interface

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;


    namespace CoreAudioApi.Interfaces
    {
        /// <summary>
        /// IMMNotificationClient
        /// </summary>
        [Guid("7991EEC9-7E89-4D85-8390-6C703CEC60C0"),
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IMMNotificationClient
        {
            /// <summary>
            /// Device State Changed
            /// </summary>
            void OnDeviceStateChanged([MarshalAs(UnmanagedType.LPWStr)] string deviceId, [MarshalAs(UnmanagedType.I4)] EDeviceState newState);

            /// <summary>
            /// Device Added
            /// </summary>

     void OnDeviceAdded([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId);

            /// <summary>
            /// Device Removed
            /// </summary>
            void OnDeviceRemoved([MarshalAs(UnmanagedType.LPWStr)] string deviceId);

            /// <summary>
            /// Default Device Changed
            /// </summary>
            void OnDefaultDeviceChanged(EDataFlow flow, ERole role, [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId);

            /// <summary>
            /// Property Value Changed
            /// </summary>
            /// <param name="pwstrDeviceId"></param>
            /// <param name="key"></param>
            void OnPropertyValueChanged([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId, PropertyKey key);
        }

    I have created CMMNotificationClient.cs class which implements above methods

    Now I am not sure How to register the call back to these methods and register for Plug and UnPlug Events

    I tried to register it in the IMMDeviceEnumerator

    [PreserveSig]
            int RegisterEndpointNotificationCallback(IMMNotificationClient pClient);
            [PreserveSig]
            int UnregisterEndpointNotificationCallback(IMMNotificationClient pClient);

    Can you please help on this or can you provide any other solution

    Thnaks

    Gourav

    Sunday, February 9, 2014 6:31 AM

All replies

  • Hi GouravSharma,

    Your best approach is probably WMI. You can subscribe to WMI events reflecting the creation/destruction of instances of most WMI classes. An instance of Win32_SoundDevic exists for each audio device. See the following link. http://msdn.microsoft.com/en-us/library/windows/desktop/aa394463%28v=vs.85%29.aspx.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 10, 2014 5:56 AM
  • Using WMI I am not able to get the audio port events it only gets the USB port events
    Tuesday, February 18, 2014 8:57 AM