-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationClientImplementation.cs
132 lines (115 loc) · 4.67 KB
/
NotificationClientImplementation.cs
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
128
129
130
131
132
using NAudio.CoreAudioApi;
using System;
namespace WinMediaPie
{
public class VolumeChangeEventArgs : EventArgs
{
public float volumePercent;
public bool isMuted;
}
class NotificationClientImplementation : NAudio.CoreAudioApi.Interfaces.IMMNotificationClient
{
public event EventHandler<VolumeChangeEventArgs> VolumeChange;
private MMDevice currentDefaultDevice = null;
private MMDeviceEnumerator mMDeviceEnumerator;
/// <summary>
/// Class that listens for audio device change and for audio volume changes, and audio playback mute/unmute
/// </summary>
/// <remarks>
/// Calling Initialize is needed after instantiating this class AND assigning a callback to <see cref="VolumeChange"/>
/// </remarks>
/// <param name="mMDeviceEnumerator">An instance of MMDeviceEnumerator that identifies the controlled sound device</param>
public NotificationClientImplementation(MMDeviceEnumerator mMDeviceEnumerator)
{
this.mMDeviceEnumerator = mMDeviceEnumerator;
currentDefaultDevice = mMDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
MaybeSetupVolumeNotificationListener();
}
/// <summary>
/// Must be called after instantiating this class AND assigning a callback to <see cref="VolumeChange"/>
/// </summary>
internal void Initialize()
{
VolumeChange(this, new VolumeChangeEventArgs
{
volumePercent = currentDefaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100,
isMuted = currentDefaultDevice.AudioEndpointVolume.Mute
});
}
/// <summary>
/// Sets the volume to specified percentage level
/// </summary>
/// <param name="newValue">New volume level (percent, 0 - 100)</param>
internal void SetVolume(float newValue)
{
currentDefaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar = newValue / 100;
}
/// <summary>
/// Unmutes the current audio device
/// </summary>
internal void Unmute()
{
currentDefaultDevice.AudioEndpointVolume.Mute = false;
}
/// <summary>
/// Mutes the current audio device
/// </summary>
internal void Mute()
{
currentDefaultDevice.AudioEndpointVolume.Mute = true;
}
/// <summary>
/// Sets up audio device's event listeners if <see cref="currentDefaultDevice"/> is not null
/// </summary>
private void MaybeSetupVolumeNotificationListener()
{
if (currentDefaultDevice != null)
{
currentDefaultDevice.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification;
}
}
/// <summary>
/// Clears audio device's event listeners if <see cref="currentDefaultDevice"/> is not null\
/// Probably needed when the default audio devices is changed
/// </summary>
private void MaybeClearVolumeNotificationListener()
{
if (currentDefaultDevice != null)
{
currentDefaultDevice.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
}
}
public void OnDefaultDeviceChanged(DataFlow dataFlow, Role role, string defaultDeviceId)
{
Console.WriteLine($"Default playback device changed to {dataFlow.ToString()}");
MaybeClearVolumeNotificationListener();
currentDefaultDevice = mMDeviceEnumerator.GetDevice(defaultDeviceId);
MaybeSetupVolumeNotificationListener();
}
private void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
Console.WriteLine($"Volume changed to {Math.Round(data.MasterVolume * 100)}%, {(data.Muted ? "muted" : "unmuted")}");
VolumeChange(this, new VolumeChangeEventArgs
{
volumePercent = data.MasterVolume * 100,
isMuted = data.Muted
});
}
public void OnDeviceAdded(string pwstrDeviceId)
{
// Do nothing
}
public void OnDeviceRemoved(string deviceId)
{
// Do nothing
}
public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
Console.WriteLine($"Device {deviceId} state changed to {newState}");
}
public void OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key)
{
// Do nothing
}
}
}