插头拔下音频设备事件

时间:2014-02-09 07:00:33

标签: c#

我正在尝试在插入或拔出音频设备时收到一个事件,但有几个问题需要它,但我仍然无法获得

我试图使用CoreAudio API我创建了ImmNotificationClient.cs接口

[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);
}

我创建了CMMNotificationClient.cs类,它实现了上述方法

现在我不确定如何将回调注册到这些方法并注册Plug and UnPlug事件

我尝试在IMMDeviceEnumerator

中注册它
[PreserveSig]
int RegisterEndpointNotificationCallback(IMMNotificationClient pClient);

[PreserveSig]
int UnregisterEndpointNotificationCallback(IMMNotificationClient pClient);

您能帮忙解决这个问题,还是可以提供其他解决方案?

1 个答案:

答案 0 :(得分:0)

您可以使用RegisterDeviceNotification Windows API获得通知。

Stackoverflow example

CodeProject example