I am struggling to implement a function to detect a new headset being connected to the windows device in a WPF application. I have tried to implement it in the following way but the code fails to trigger when connecting any sound device (testing using USB headsets currently as jack sockets are not available in the windows devices currently in use).
Following previous postings on here I have implemented the following :-
In my MainWindow Class :-
//Create sound listener for new devices
public MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
public NotificationClientImplementation notificationClient;
public IMMNotificationClient notifyClient;
public int RegisterEndpointNotificationCallback([In][MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
{
//DeviceEnum declared below
return deviceEnum.RegisterEndpointNotificationCallback(client);
}
and I have a separate class for NotificationClientImplementation :-
public class NotificationClientImplementation : IMMNotificationClient
{
public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId)
{
//Do some Work
}
public void OnDeviceAdded(string deviceId)
{
//Do some Work
((MainWindow)System.Windows.Application.Current.MainWindow).SoundNewAction();
}
public void OnDeviceRemoved(string deviceId)
{
//Do some Work
}
public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
//Do some Work
}
public void OnPropertyValueChanged(string deviceId, PropertyKey propertyKey)
{
//Do some Work
}
}
I have then implemented back in the main window the following to do the work :-
public void SoundNewAction()
{
m_notifyIcon.BalloonTipText = "A New Sound Device has been connected";
m_notifyIcon.BalloonTipTitle = "New Sound Device;
m_notifyIcon.BalloonTipClicked += new EventHandler(BalloonTipClicked);
m_notifyIcon.ShowBalloonTip(2000);
}
For some reason the code is never triggered, I have tried connecting new devices, devices already known to the machine and every option I can find but it doesn't trigger the action.
I know I am missing something simple but I can't for the life of me find it.
Thanks in advance.