-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioControl.cs
More file actions
63 lines (58 loc) · 1.46 KB
/
AudioControl.cs
File metadata and controls
63 lines (58 loc) · 1.46 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
using NAudio.CoreAudioApi;
namespace AutoLogout
{
public class AudioControl
{
private readonly MMDeviceEnumerator deviceEnumerator;
private MMDevice? defaultDevice;
private bool? previousState = null;
public readonly System.Windows.Forms.Timer timer;
public AudioControl() {
// Initialize the CoreAudio components
deviceEnumerator = new MMDeviceEnumerator();
timer = new System.Windows.Forms.Timer
{
Interval = 1000
};
timer.Tick += Mute;
}
public void Mute(object? sender, EventArgs? e)
{
Mute();
}
public void Mute()
{
try
{
defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
}
catch
{
Console.WriteLine("Failed to get default audio output device.");
return;
}
if(previousState == null) {
defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
previousState = defaultDevice.AudioEndpointVolume.Mute;
}
foreach(var device in deviceEnumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)) {
device.AudioEndpointVolume.Mute = true;
}
if(!timer.Enabled) {
timer.Start();
}
}
public void Unmute()
{
if (previousState == false && defaultDevice is not null)
{
foreach (var device in deviceEnumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
device.AudioEndpointVolume.Mute = false;
}
}
previousState = null;
timer.Stop();
}
}
}