-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioDeviceChangeNotifier.cs
More file actions
127 lines (108 loc) · 4.98 KB
/
AudioDeviceChangeNotifier.cs
File metadata and controls
127 lines (108 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Visualizer
{
// Based on some sample code found here:
// https://stackoverflow.com/questions/6163119/handling-changed-audio-device-event-in-c-sharp
internal class AudioDeviceChangeNotifier : NAudio.CoreAudioApi.Interfaces.IMMNotificationClient, IDisposable
{
private NAudio.CoreAudioApi.MMDeviceEnumerator _deviceEnum = new NAudio.CoreAudioApi.MMDeviceEnumerator();
public AudioDeviceChangeNotifier()
{
if (System.Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
_deviceEnum.RegisterEndpointNotificationCallback(this);
}
public delegate void DefaultDeviceChangedHandler(DataFlow dataFlow, Role deviceRole, string defaultDeviceId);
/// <summary>
/// Raised when the default audio device is changed.
/// </summary>
public event DefaultDeviceChangedHandler DefaultDeviceChanged;
/// <summary>
/// Triggered by NAudio.CoreAudioApi.MMDeviceEnumerator when the default device changes.
/// </summary>
/// <param name="dataFlow"></param>
/// <param name="deviceRole"></param>
/// <param name="defaultDeviceId"></param>
public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId)
{
Debug.WriteLine($"AudioDeviceChangeNotifier::OnDefaultDeviceChanged - dataFlow: {dataFlow}, deviceRole: {deviceRole}, defaultDeviceId: {defaultDeviceId}");
if (DefaultDeviceChanged != null)
DefaultDeviceChanged(dataFlow, deviceRole, defaultDeviceId);
}
public delegate void DeviceAddedHandler(string deviceId);
/// <summary>
/// Raised when a new audio device is added.
/// </summary>
public event DeviceAddedHandler DeviceAdded;
/// <summary>
/// Triggered by NAudio.CoreAudioApi.MMDeviceEnumerator when an audio device is added.
/// </summary>
/// <param name="deviceId"></param>
public void OnDeviceAdded(string deviceId)
{
Debug.WriteLine($"AudioDeviceChangeNotifier::OnDeviceAdded - deviceId: {deviceId}");
if (DeviceAdded != null)
DeviceAdded(deviceId);
}
//public delegate void DeviceRemovedHandler(string deviceId);
///// <summary>
///// Raised when an audio device is removed.
///// </summary>
//public event DeviceRemovedHandler DeviceRemoved;
/// <summary>
/// Triggered by NAudio.CoreAudioApi.MMDeviceEnumerator when an audio device is removed.
/// </summary>
/// <param name="deviceId"></param>
public void OnDeviceRemoved(string deviceId)
{
Debug.WriteLine($"AudioDeviceChangeNotifier::OnDeviceRemoved - deviceId: {deviceId}");
if (DeviceAdded != null)
DeviceAdded(deviceId);
}
public delegate void DeviceStateChangedHandler(string deviceId, DeviceState newState);
/// <summary>
/// Raised when an audio device's state is changed.
/// </summary>
public event DeviceStateChangedHandler DeviceStateChanged;
/// <summary>
/// Triggered by NAudio.CoreAudioApi.MMDeviceEnumerator when an audio device's state is changed.
/// </summary>
/// <param name="deviceId"></param>
/// <param name="newState"></param>
public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
Debug.WriteLine($"AudioDeviceChangeNotifier::OnDeviceStateChanged - deviceId: {deviceId}, newState: {newState}");
if (DeviceStateChanged != null)
DeviceStateChanged(deviceId, newState);
}
public delegate void PropertyValueChangedHandler(string deviceId);
/// <summary>
/// Raised when a property value is changed.
/// </summary>
public event PropertyValueChangedHandler PropertyValueChanged;
/// <summary>
/// Triggered by NAudio.CoreAudioApi.MMDeviceEnumerator when an audio device's property is changed.
/// </summary>
/// <param name="deviceId"></param>
/// <param name="propertyKey"></param>
public void OnPropertyValueChanged(string deviceId, PropertyKey propertyKey)
{
Debug.WriteLine($"AudioDeviceChangeNotifier::OnPropertyValueChanged - deviceId: {deviceId}, propertyKey: {propertyKey}");
if (PropertyValueChanged != null)
PropertyValueChanged(deviceId);
}
public void Dispose()
{
_deviceEnum.UnregisterEndpointNotificationCallback(this);
}
}
}