-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaControlBar.xaml.cs
321 lines (283 loc) · 10.8 KB
/
MediaControlBar.xaml.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
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Globalization;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using Windows.Media.Control;
using Windows.Storage.Streams;
namespace GlobalMediaControl
{
/// <summary>
/// Interaction logic for GlobalMediaControl.xaml
/// </summary>
public partial class MediaControlBar : UserControl
{
public static readonly ImageSource DefaultAlbumImgSrc = new BitmapImage(new Uri("pack://application:,,,/GlobalMediaControl;component/Resources/album-32.jpg"));
private readonly Storyboard SlideUp, SlideDown, Fade, Marquee;
private readonly DoubleAnimation TitleAnime;
private bool LastSkipActionIsPrev = false;
private GlobalSystemMediaTransportControlsSessionManager SMTCManager;
private GlobalSystemMediaTransportControlsSession SMTCSession { get; set; }
public GlobalSystemMediaTransportControlsSessionMediaProperties MediaProps { get; internal set; }
public GlobalSystemMediaTransportControlsSessionPlaybackInfo PlayBackInfo { get; internal set; }
public ImageSource AlbumImgSrc { get; internal set; }
public string ArtistLine
{
get => MediaProps == null ? null : MediaProps.Artist + " - " + MediaProps.AlbumTitle;
internal set { }
}
public double DesiredTextSize { get; internal set; }
public MediaControlBar()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
File.WriteAllText("D:\\media_control.log", "loaded\n");
Dispatcher.InvokeAsync(InitMediaSessionManager);
// InitMediaSessionManager();
// freeze to avoid leaking, see: https://stackoverflow.com/questions/799911/in-what-scenarios-does-freezing-wpf-objects-benefit-performance-greatly
DefaultAlbumImgSrc.Freeze();
AlbumImgSrc = DefaultAlbumImgSrc;
TitleAnime = new DoubleAnimation
{
From = 0,
To = 0,
Duration = new Duration(TimeSpan.FromSeconds(3)),
RepeatBehavior = new RepeatBehavior(1),
FillBehavior = FillBehavior.Stop
};
Fade = (Storyboard)Resources["fadeStoryBoard"];
Marquee = (Storyboard)Resources["marqueeStoryBoard"];
SlideUp = (Storyboard)Resources["slideUpStoryBoard"];
SlideDown = (Storyboard)Resources["slideDownStoryBoard"];
SlideUp.Completed += (s, e) => LastSkipActionIsPrev = false;
SlideDown.Completed += (s, e) => LastSkipActionIsPrev = false;
// SlideUp.Completed += (s, e) => OnTitleLableSizeChanged(null, null);
// SlideDown.Completed += (s, e) => OnTitleLableSizeChanged(null, null);
// canvas.SizeChanged += OnTitleLableSizeChanged;
}
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
File.AppendAllText("D:\\media_control.log", e.ExceptionObject.ToString());
}
private void UpdateUI(Action act)
{
Dispatcher.Invoke(act);
}
private async void InitMediaSessionManager()
{
SMTCManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
if (SMTCManager == null)
{
return;
}
SMTCManager.CurrentSessionChanged += OnCurrentSessionChanged;
OnCurrentSessionChanged(SMTCManager, null);
}
private void OnCurrentSessionChanged(GlobalSystemMediaTransportControlsSessionManager sender, CurrentSessionChangedEventArgs args)
{
if (sender == null)
{
return;
}
SMTCSession = sender.GetCurrentSession();
if (SMTCSession != null)
{
SMTCSession.MediaPropertiesChanged += OnMediaPropertiesChanged;
SMTCSession.PlaybackInfoChanged += OnPlaybackInfoChanged;
OnMediaPropertiesChanged(SMTCSession, null);
OnPlaybackInfoChanged(SMTCSession, null);
}
}
private void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender, PlaybackInfoChangedEventArgs args)
{
if (sender == null)
{
return;
}
PlayBackInfo = sender.GetPlaybackInfo();
if (PlayBackInfo != null)
{
UpdateUI(() =>
{
bool playing = PlayBackInfo.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;
playImg.Source = playing ? pauseIcon.Source : playIcon.Source;
});
}
}
private async void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args)
{
if (sender == null)
{
return;
}
try
{
MediaProps = await sender.TryGetMediaPropertiesAsync();
}
catch
{
return;
}
if (MediaProps != null)
{
if (MediaProps.Thumbnail != null)
{
IRandomAccessStreamWithContentType stream = await MediaProps.Thumbnail.OpenReadAsync();
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
var data = (await decoder.GetPixelDataAsync()).DetachPixelData();
UpdateUI(() =>
{
try
{
AlbumImgSrc = System.Windows.Media.Imaging.BitmapSource.Create(
(int)decoder.PixelWidth, (int)decoder.PixelHeight,
decoder.DpiX, decoder.DpiY,
PixelFormats.Bgra32,
null,
data,
(int)decoder.PixelWidth * 4
);
}
catch
{
AlbumImgSrc = DefaultAlbumImgSrc;
}
});
}
else
{
UpdateUI(() => { AlbumImgSrc = DefaultAlbumImgSrc; });
}
UpdateUI(() =>
{
albumImg.Source = AlbumImgSrc;
albumImg.Source.Freeze();
titleBlock.Text = MediaProps.Title;
artistBlock.Text = ArtistLine;
if (LastSkipActionIsPrev)
{
SlideDown.Begin(textGrid);
}
else
{
SlideUp.Begin(textGrid);
}
});
}
}
private void ToggleControls()
{
if (textGrid.Visibility == Visibility.Visible)
{
textGrid.Visibility = Visibility.Hidden;
actionGrid.Visibility = Visibility.Visible;
}
else
{
textGrid.Visibility = Visibility.Visible;
actionGrid.Visibility = Visibility.Hidden;
}
Fade.Begin(contentGrid);
}
private void SkipNowPlaying(bool next)
{
if (next)
OnNextBtnClick(null, null);
else
OnPrevBtnClick(null, null);
}
private async void OnPrevBtnClick(object sender, RoutedEventArgs e)
{
if (SMTCSession == null)
{
return;
}
LastSkipActionIsPrev = await SMTCSession.TrySkipPreviousAsync();
}
private async void OnNextBtnClick(object sender, RoutedEventArgs e)
{
if (SMTCSession == null)
{
return;
}
await SMTCSession.TrySkipNextAsync();
}
private async void OnPlayBtnClick(object sender, RoutedEventArgs e)
{
if (SMTCSession == null)
{
return;
}
await SMTCSession.TryTogglePlayPauseAsync();
}
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
SkipNowPlaying(e.Delta > 0);
}
private void OnToolTipLoad(object sender, RoutedEventArgs e)
{
var child = (FrameworkElement)sender;
while (child != null)
{
if (child.GetType() == typeof(ToolTip))
{
AcrylicWindowHelper.EnableBlur(child);
break;
}
child = child.Parent as FrameworkElement;
}
}
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
{
ToggleControls();
}
else if (e.ChangedButton == MouseButton.Middle)
{
OnPlayBtnClick(sender, new RoutedEventArgs());
}
}
private void OnTitleLableSizeChanged(object sender, SizeChangedEventArgs e)
{
double offset = titleBlock.DesiredSize.Width - canvas.RenderSize.Width;
if (offset <= 0)
{
return;
}
var story = new Storyboard();
var anime = TitleAnime.Clone();
anime.To = -offset;
story.Children.Add(anime);
Storyboard.SetTargetName(anime, "marquee");
Storyboard.SetTargetProperty(anime, new PropertyPath(TranslateTransform.XProperty));
story.Begin(titleBlock);
}
private Size MeasureString(string candidate)
{
var typeFace = new Typeface(
titleBlock.FontFamily,
titleBlock.FontStyle,
titleBlock.FontWeight,
titleBlock.FontStretch);
var culture = new CultureInfo("zh-CN");
var formattedText = new FormattedText(
candidate,
culture,
FlowDirection.LeftToRight,
typeFace,
titleBlock.FontSize,
Brushes.Black,
new NumberSubstitution(),
1);
return new Size(formattedText.Width, formattedText.Height);
}
}
}