-
Notifications
You must be signed in to change notification settings - Fork 4
/
MediaControlsPlugin.cs
140 lines (125 loc) · 3.41 KB
/
MediaControlsPlugin.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
133
134
135
136
137
138
139
140
using SuchByte.MacroDeck.Plugins;
using System.Collections.Generic;
using System;
using System.Timers;
using System.Threading.Tasks;
using SuchByte.MacroDeck.Variables;
using Windows.Media.Control;
namespace MediaControls_Plugin;
public class MediaControlsPlugin : MacroDeckPlugin
{
private GlobalSystemMediaTransportControlsSessionManager _manager;
private GlobalSystemMediaTransportControlsSession _session;
private Timer _timeDateTimer;
public override async void Enable()
{
Actions = new List<PluginAction>
{
new MediaPlayPauseAction(),
new MediaNextAction(),
new MediaPrevAction(),
new MediaVolUpAction(),
new MediaVolDownAction(),
new MediaVolMuteAction(),
};
await InitializeSessionManager();
_timeDateTimer = new Timer(1000)
{
Enabled = true
};
_timeDateTimer.Elapsed += OnTimerTick;
_timeDateTimer.Start();
}
private async void Manager_SessionsChanged(
GlobalSystemMediaTransportControlsSessionManager sender,
SessionsChangedEventArgs args)
{
await UpdateSession();
}
private async void OnTimerTick(object sender, EventArgs e)
{
UpdateVolumeLevel();
UpdateVolumeMuteState();
await UpdatePlayingTitleAsync();
}
private async Task InitializeSessionManager()
{
_manager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
if (_manager != null)
{
_manager.SessionsChanged += Manager_SessionsChanged;
}
await UpdateSession();
}
private async Task UpdateSession()
{
if (_manager != null)
{
_session = _manager.GetCurrentSession();
if (_session != null)
{
await UpdatePlayingTitleAsync();
}
}
else
{
_session = null;
}
}
private async Task UpdatePlayingTitleAsync()
{
if (_session == null)
{
return;
}
var currentTile = string.Empty;
var currentArtist = string.Empty;
try
{
var info = await _session.TryGetMediaPropertiesAsync();
currentTile = info?.Title ?? "-";
currentArtist = info?.Artist ?? "-";
}
catch
{
currentTile = currentArtist = "Unknown";
}
finally
{
VariableManager.SetValue("current_playing_title", currentTile, VariableType.String, this, null);
VariableManager.SetValue("current_playing_artist", currentArtist, VariableType.String, this, null);
}
}
private void UpdateVolumeLevel()
{
var volume = 0d;
try
{
volume = Math.Round(AudioManager.GetMasterVolume());
}
catch
{
volume = 0;
}
finally
{
VariableManager.SetValue("volume_level", volume, VariableType.Integer, this, null);
}
}
private void UpdateVolumeMuteState()
{
var isMuted = false;
try
{
isMuted = AudioManager.GetMasterVolumeMute();
}
catch
{
isMuted = false;
}
finally
{
VariableManager.SetValue("volume_muted", isMuted, VariableType.Bool, this, null);
}
}
}