-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMediaControlsPlugin.cs
More file actions
207 lines (185 loc) · 6.02 KB
/
Copy pathMediaControlsPlugin.cs
File metadata and controls
207 lines (185 loc) · 6.02 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using SuchByte.MacroDeck.Plugins;
using System.Collections.Generic;
using System;
using System.Timers;
using System.Threading.Tasks;
using Windows.Media;
using SuchByte.MacroDeck.Variables;
using Windows.Media.Control;
namespace MediaControls_Plugin;
public static class PluginInstance
{
public static MediaControlsPlugin Main;
}
public class MediaControlsPlugin : MacroDeckPlugin
{
public static GlobalSystemMediaTransportControlsSessionManager Manager;
private GlobalSystemMediaTransportControlsSession _session;
private Timer _timeDateTimer;
private AudioManager SpeakerManager = new AudioManager(Mode.Speakers);
private AudioManager MicrophoneManager = new AudioManager(Mode.Microphone);
public MediaControlsPlugin()
{
PluginInstance.Main = this;
}
public override async void Enable()
{
Actions = new List<PluginAction>
{
new MediaPlayPauseAction(),
new MediaNextAction(),
new MediaPrevAction(),
new MediaVolUpAction(),
new MediaVolDownAction(),
new MediaVolMuteAction(),
new MediaMicMuteAction(),
new MediaFastForwardAction(),
new MediaRewindAction(),
new MediaShuffleToggle(),
new MediaLoopOff(),
new MediaLoopList(),
new MediaLoopTrack(),
};
await InitializeSessionManager();
_timeDateTimer = new Timer(300)
{
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();
await UpdatePlayerStateAsync();
}
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();
await UpdatePlayerStateAsync();
}
}
else
{
_session = null;
}
}
private async Task UpdatePlayerStateAsync()
{
if (_session == null)
{
return;
}
var isPlaying = false;
var playbackStatus = GlobalSystemMediaTransportControlsSessionPlaybackStatus.Closed;
var shuffle = false;
var loop = MediaPlaybackAutoRepeatMode.None;
try
{
var info = await Task.Run(_session.GetPlaybackInfo);
playbackStatus = info?.PlaybackStatus ?? GlobalSystemMediaTransportControlsSessionPlaybackStatus.Closed;
isPlaying = playbackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;
shuffle = info?.IsShuffleActive ?? false;
loop = info?.AutoRepeatMode ?? MediaPlaybackAutoRepeatMode.None;
}
catch
{
isPlaying = shuffle = false;
playbackStatus = GlobalSystemMediaTransportControlsSessionPlaybackStatus.Closed;
loop = MediaPlaybackAutoRepeatMode.None;
}
finally
{
VariableManager.SetValue("is_playing", isPlaying, VariableType.Bool, this, null!);
VariableManager.SetValue("is_shuffled", shuffle, VariableType.Bool, this, null!);
VariableManager.SetValue("playback_status", (int) playbackStatus, VariableType.Integer, this, null!);
VariableManager.SetValue("loop_status", (int) loop, VariableType.Integer, this, 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 speakerVolume = 0d;
var micVolume = 0d;
try
{
speakerVolume = Math.Round(SpeakerManager.GetMasterVolume());
micVolume = Math.Round(MicrophoneManager.GetMasterVolume());
}
catch
{
speakerVolume = 0;
micVolume = 0;
}
finally
{
VariableManager.SetValue("speaker_volume_level", speakerVolume, VariableType.Integer, this, null);
VariableManager.SetValue("mic_volume_level", micVolume, VariableType.Integer, this, null);
}
}
private void UpdateVolumeMuteState()
{
var isSpeakerMuted = false;
var isMicMuted = false;
try
{
isSpeakerMuted = SpeakerManager.GetMasterVolumeMute();
isMicMuted = MicrophoneManager.GetMasterVolumeMute();
}
catch
{
isSpeakerMuted = false;
isMicMuted = false;
}
finally
{
VariableManager.SetValue("speaker_muted", isSpeakerMuted, VariableType.Bool, this, null);
VariableManager.SetValue("mic_muted", isMicMuted, VariableType.Bool, this, null);
}
}
}