-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioDeviceService.cs
More file actions
70 lines (61 loc) · 2.2 KB
/
AudioDeviceService.cs
File metadata and controls
70 lines (61 loc) · 2.2 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace AudioSwitcher;
public class AudioDeviceService
{
private static readonly PropertyKey PKEY_FriendlyName =
new(new Guid("A45C254E-DF1C-4EFD-8020-67D146A850E0"), 14);
private readonly IMMDeviceEnumerator _enumerator;
private readonly IPolicyConfig _policyConfig;
public AudioDeviceService()
{
_enumerator = (IMMDeviceEnumerator)new MMDeviceEnumeratorCom();
_policyConfig = (IPolicyConfig)new PolicyConfigCom();
}
public List<(string Id, string Name)> GetPlaybackDevices()
{
var results = new List<(string, string)>();
_enumerator.EnumAudioEndpoints(EDataFlow.eRender, 0x00000001, out var collection);
collection.GetCount(out int count);
for (int i = 0; i < count; i++)
{
collection.Item(i, out var device);
device.GetId(out string id);
results.Add((id, GetDeviceFriendlyName(device)));
}
return results;
}
public string GetDefaultDeviceId()
{
_enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, out var device);
device.GetId(out string id);
return id;
}
public void SetDefaultDevice(string deviceId)
{
_policyConfig.SetDefaultEndpoint(deviceId, ERole.eConsole);
_policyConfig.SetDefaultEndpoint(deviceId, ERole.eMultimedia);
_policyConfig.SetDefaultEndpoint(deviceId, ERole.eCommunications);
}
public string Toggle(string deviceA, string deviceB)
{
string currentId = GetDefaultDeviceId();
string targetId = currentId == deviceA ? deviceB : deviceA;
SetDefaultDevice(targetId);
var devices = GetPlaybackDevices();
var match = devices.Find(d => d.Id == targetId);
return match.Name ?? "Unknown device";
}
private string GetDeviceFriendlyName(IMMDevice device)
{
try
{
device.OpenPropertyStore(0, out var store);
var key = PKEY_FriendlyName;
store.GetValue(ref key, out var value);
return value.StringValue ?? "Unknown";
}
catch { return "Unknown"; }
}
}