-
Notifications
You must be signed in to change notification settings - Fork 191
/
AudioFlyoutHelper.cs
367 lines (294 loc) · 11.1 KB
/
AudioFlyoutHelper.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using ModernFlyouts.Utilities;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using System;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Windows.Media.Control;
namespace ModernFlyouts
{
public class AudioFlyoutHelper : FlyoutHelperBase
{
private AudioDeviceNotificationClient client;
private MMDeviceEnumerator enumerator;
private MMDevice device;
private VolumeControl volumeControl;
private SessionsPanel sessionsPanel;
private bool _isinit = false;
private bool _SMTCAvail = false;
public override event ShowFlyoutEventHandler ShowFlyoutRequested;
public AudioFlyoutHelper()
{
Initialize();
}
public void Initialize()
{
AlwaysHandleDefaultFlyout = true;
#region Creating Volume Control
volumeControl = new VolumeControl();
volumeControl.VolumeButton.Click += VolumeButton_Click;
volumeControl.VolumeSlider.ValueChanged += VolumeSlider_ValueChanged;
volumeControl.VolumeSlider.PreviewMouseWheel += VolumeSlider_PreviewMouseWheel;
#endregion
#region Creating Session Controls
sessionsPanel = new SessionsPanel();
SecondaryContent = sessionsPanel;
try { SetupSMTCAsync(); } catch { }
#endregion
PrimaryContent = volumeControl;
client = new AudioDeviceNotificationClient();
enumerator = new MMDeviceEnumerator();
enumerator.RegisterEndpointNotificationCallback(client);
if (enumerator.HasDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia))
{
UpdateDevice(enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia));
}
OnEnabled();
_isinit = true;
}
public void OnExternalUpdated(bool isMediaKey)
{
if (!isMediaKey || (isMediaKey && _SMTCAvail))
ShowFlyoutRequested?.Invoke(this);
}
#region Volume
private void Client_DefaultDeviceChanged(object sender, string e)
{
if (e != null)
{
UpdateDevice(enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia));
}
else
{
UpdateDevice(null);
}
}
private void UpdateDevice(MMDevice mmdevice)
{
if (device != null)
{
device.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
}
device = mmdevice;
if (device != null)
{
UpdateVolume(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
device.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification;
PrimaryContentVisible = true;
}
else { PrimaryContentVisible = false; }
}
private void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
UpdateVolume(data.MasterVolume * 100);
}
private bool _isInCodeValueChange = false; //Prevents a LOOP between changing volume.
private void UpdateVolume(double volume)
{
volumeControl.Dispatcher.Invoke(() =>
{
UpdateVolumeGlyph(volume);
_isInCodeValueChange = true;
volumeControl.VolumeSlider.Value = Math.Round(volume);
_isInCodeValueChange = false;
volumeControl.textVal.Text = Math.Round(volume).ToString("00");
});
}
private void UpdateVolumeGlyph(double volume)
{
if (device != null && !device.AudioEndpointVolume.Mute)
{
volumeControl.VolumeShadowGlyph.Visibility = Visibility.Visible;
if (volume >= 66)
volumeControl.VolumeGlyph.Glyph = CommonGlyphs.Volume3;
else if (volume < 1)
volumeControl.VolumeGlyph.Glyph = CommonGlyphs.Volume0;
else if (volume < 33)
volumeControl.VolumeGlyph.Glyph = CommonGlyphs.Volume1;
else if (volume < 66)
volumeControl.VolumeGlyph.Glyph = CommonGlyphs.Volume2;
}
else
{
volumeControl.VolumeShadowGlyph.Visibility = Visibility.Collapsed;
volumeControl.VolumeGlyph.Glyph = CommonGlyphs.Mute;
}
}
private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!_isInCodeValueChange)
{
var value = Math.Truncate(e.NewValue);
var oldValue = Math.Truncate(e.OldValue);
if (value == oldValue)
{
return;
}
if (device != null)
{
device.AudioEndpointVolume.MasterVolumeLevelScalar = (float)(value / 100);
device.AudioEndpointVolume.Mute = false;
}
e.Handled = true;
}
}
private void VolumeButton_Click(object sender, RoutedEventArgs e)
{
if (device != null)
{
device.AudioEndpointVolume.Mute = !device.AudioEndpointVolume.Mute;
}
}
private void VolumeSlider_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
var slider = sender as Slider;
var value = Math.Truncate(slider.Value);
var change = e.Delta / 120;
var volume = value + change;
if (volume > 100 || volume < 0)
{
return;
}
if (device != null)
{
device.AudioEndpointVolume.MasterVolumeLevelScalar = (float)(volume / 100);
device.AudioEndpointVolume.Mute = false;
}
e.Handled = true;
}
#endregion
#region SMTC
private object _SMTC;
[MethodImpl(MethodImplOptions.NoInlining)]
private async void SetupSMTCAsync()
{
GlobalSystemMediaTransportControlsSessionManager SMTC;
SMTC = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
SMTC.SessionsChanged += SMTC_SessionsChanged;
_SMTC = SMTC;
LoadSessionControls();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void DetachSMTC()
{
if (_SMTC is GlobalSystemMediaTransportControlsSessionManager SMTC)
{
SMTC.SessionsChanged -= SMTC_SessionsChanged;
_SMTC = null;
}
ClearSessionControls();
SecondaryContentVisible = false;
}
private void ClearSessionControls()
{
foreach (var child in sessionsPanel.SessionsStackPanel.Children)
{
var s = child as SessionControl;
s?.DisposeSession();
}
sessionsPanel.SessionsStackPanel.Children.Clear();
_SMTCAvail = false;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void SMTC_SessionsChanged(GlobalSystemMediaTransportControlsSessionManager sender, SessionsChangedEventArgs args)
{
Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(LoadSessionControls));
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void LoadSessionControls()
{
ClearSessionControls();
if (!IsEnabled)
{
return;
}
if (_SMTC is GlobalSystemMediaTransportControlsSessionManager SMTC)
{
var sessions = SMTC.GetSessions();
foreach (var session in sessions)
{
sessionsPanel.SessionsStackPanel.Children.Add(new SessionControl
{
SMTCSession = session
});
}
if (sessionsPanel.SessionsStackPanel.Children.Count > 0)
{
SecondaryContentVisible = true;
_SMTCAvail = true;
}
}
}
#endregion
#region SMTC Thumbnails
public static ImageSource GetDefaultAudioThumbnail()
{
return new BitmapImage(PackUriHelper.GetAbsoluteUri("Assets/Images/DefaultAudioThumbnail.png"));
}
public static ImageSource GetDefaultImageThumbnail()
{
return new BitmapImage(PackUriHelper.GetAbsoluteUri("Assets/Images/DefaultImageThumbnail.png"));
}
public static ImageSource GetDefaultVideoThumbnail()
{
return new BitmapImage(PackUriHelper.GetAbsoluteUri("Assets/Images/DefaultVideoThumbnail.png"));
}
#endregion
protected override void OnEnabled()
{
base.OnEnabled();
AppDataHelper.AudioModuleEnabled = IsEnabled;
if (!IsEnabled)
{
return;
}
client.DefaultDeviceChanged += Client_DefaultDeviceChanged;
if (device != null)
{
device.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification;
PrimaryContentVisible = true;
}
else { PrimaryContentVisible = false; }
if (_isinit)
{
try { SetupSMTCAsync(); } catch { }
}
}
protected override void OnDisabled()
{
base.OnDisabled();
client.DefaultDeviceChanged -= Client_DefaultDeviceChanged;
if (device != null)
{
device.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification;
}
PrimaryContentVisible = false;
try { DetachSMTC(); } catch { }
AppDataHelper.AudioModuleEnabled = IsEnabled;
}
}
public class AudioDeviceNotificationClient : IMMNotificationClient
{
public event EventHandler<string> DefaultDeviceChanged;
public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId)
{
if (dataFlow == DataFlow.Render && deviceRole == Role.Multimedia)
{
DefaultDeviceChanged?.Invoke(this, defaultDeviceId);
}
}
public void OnDeviceAdded(string deviceId)
{ }
public void OnDeviceRemoved(string deviceId)
{ }
public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{ }
public void OnPropertyValueChanged(string deviceId, PropertyKey propertyKey)
{ }
}
}