cu-trainer/Ultra Paradise Application/src/NowPlaying.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Drawing; |
| 4 | using System.Drawing.Drawing2D; |
| 5 | using System.Drawing.Text; |
| 6 | using System.Globalization; |
| 7 | using System.IO; |
| 8 | using System.Threading.Tasks; |
| 9 | using System.Windows.Forms; |
| 10 | using Windows.Media; |
| 11 | using Windows.Media.Control; |
| 12 | |
| 13 | namespace UltraParadise |
| 14 | { |
| 15 | internal class NowPlayingCard : Control |
| 16 | { |
| 17 | public bool Has, Found, Playing, Shuffle, CanShuffle, CanRepeat, CanSeek; |
| 18 | public int Repeat; |
| 19 | public string Title = "", Artist = "", Album = "", App = "", FallbackApp = "", FallbackTitle = ""; |
| 20 | public TimeSpan Pos, End; |
| 21 | public DateTimeOffset Stamp; |
| 22 | public Image Thumb; |
| 23 | public event Action<string> Command; |
| 24 | public event Action<double> Seek; |
| 25 | |
| 26 | private readonly Dictionary<string, Rectangle> btns = new Dictionary<string, Rectangle>(); |
| 27 | private Rectangle bar; |
| 28 | private string hover; |
| 29 | private bool hoverBar; |
| 30 | private readonly Font head = Theme.F("Segoe UI Semibold", 8f); |
| 31 | private readonly Font jam = Theme.F("Segoe UI Semibold", 11f); |
| 32 | private readonly Font title = Theme.F("Segoe UI Semibold", 13f); |
| 33 | private readonly Font artist = Theme.F("Segoe UI", 10f); |
| 34 | private readonly Font small = Theme.F("Segoe UI", 8.5f); |
| 35 | private Font icons, iconsBig; |
| 36 | |
| 37 | public NowPlayingCard() |
| 38 | { |
| 39 | SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true); |
| 40 | try |
| 41 | { |
| 42 | icons = new Font("Segoe MDL2 Assets", 13f * Theme.Scale); |
| 43 | iconsBig = new Font("Segoe MDL2 Assets", 17f * Theme.Scale); |
| 44 | if (icons.Name != "Segoe MDL2 Assets") { icons = null; iconsBig = null; } |
| 45 | } |
| 46 | catch (Exception) { icons = null; iconsBig = null; } |
| 47 | } |
| 48 | |
| 49 | public void SetThumb(Image img) |
| 50 | { |
| 51 | if (Thumb != null) Thumb.Dispose(); |
| 52 | Thumb = img; |
| 53 | Invalidate(); |
| 54 | } |
| 55 | |
| 56 | public void Clear() |
| 57 | { |
| 58 | Has = false; |
| 59 | Title = Artist = Album = App = ""; |
| 60 | SetThumb(null); |
| 61 | } |
| 62 | |
| 63 | public TimeSpan LivePos() |
| 64 | { |
| 65 | TimeSpan p = Pos; |
| 66 | if (Playing && Stamp != default(DateTimeOffset)) |
| 67 | { |
| 68 | TimeSpan d = DateTimeOffset.Now - Stamp; |
| 69 | if (d > TimeSpan.Zero && d < TimeSpan.FromHours(3)) p += d; |
| 70 | } |
| 71 | if (End > TimeSpan.Zero && p > End) p = End; |
| 72 | if (p < TimeSpan.Zero) p = TimeSpan.Zero; |
| 73 | return p; |
| 74 | } |
| 75 | |
| 76 | private static string Clock(TimeSpan t) |
| 77 | { |
| 78 | int s = (int)t.TotalSeconds; |
| 79 | return s >= 3600 ? (s / 3600) + ":" + (s / 60 % 60).ToString("00", CultureInfo.InvariantCulture) + ":" + (s % 60).ToString("00", CultureInfo.InvariantCulture) |
| 80 | : (s / 60).ToString(CultureInfo.InvariantCulture) + ":" + (s % 60).ToString("00", CultureInfo.InvariantCulture); |
| 81 | } |
| 82 | |
| 83 | private static GraphicsPath Round(Rectangle r, int rad) |
| 84 | { |
| 85 | int d = Math.Max(1, rad * 2); |
| 86 | GraphicsPath p = new GraphicsPath(); |
| 87 | p.AddArc(r.X, r.Y, d, d, 180, 90); |
| 88 | p.AddArc(r.Right - d, r.Y, d, d, 270, 90); |
| 89 | p.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90); |
| 90 | p.AddArc(r.X, r.Bottom - d, d, d, 90, 90); |
| 91 | p.CloseFigure(); |
| 92 | return p; |
| 93 | } |
| 94 | |
| 95 | private void Glyph(Graphics g, string id, char glyph, string fallback, Rectangle r, bool on, bool enabled, bool big) |
| 96 | { |
| 97 | btns[id] = r; |
| 98 | bool hv = hover == id && enabled; |
| 99 | Color c = !enabled ? Theme.Faint : on ? Theme.Orange : hv ? Theme.Bright : Theme.Text; |
| 100 | StringFormat m = new StringFormat(); m.Alignment = StringAlignment.Center; m.LineAlignment = StringAlignment.Center; |
| 101 | if (hv) using (SolidBrush hb = new SolidBrush(Theme.Hover)) g.FillEllipse(hb, r); |
| 102 | using (SolidBrush b = new SolidBrush(c)) |
| 103 | { |
| 104 | if (icons != null) g.DrawString(glyph.ToString(), big ? iconsBig : icons, b, r, m); |
| 105 | else g.DrawString(fallback, small, b, r, m); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | protected override void OnPaint(PaintEventArgs e) |
| 110 | { |
| 111 | Graphics g = e.Graphics; |
| 112 | g.SmoothingMode = SmoothingMode.AntiAlias; |
| 113 | g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; |
| 114 | g.Clear(Theme.Back); |
| 115 | btns.Clear(); |
| 116 | bar = Rectangle.Empty; |
| 117 | int W = Width, pad = Theme.S(16); |
| 118 | Rectangle card = new Rectangle(0, 0, W - 1, Height - 1); |
| 119 | using (GraphicsPath p = Round(card, Theme.S(10))) |
| 120 | { |
| 121 | using (SolidBrush b = new SolidBrush(Theme.Panel)) g.FillPath(b, p); |
| 122 | using (Pen pe = new Pen(Theme.Line)) g.DrawPath(pe, p); |
| 123 | } |
| 124 | StringFormat c = new StringFormat(); c.Alignment = StringAlignment.Center; c.LineAlignment = StringAlignment.Center; c.Trimming = StringTrimming.EllipsisCharacter; c.FormatFlags = StringFormatFlags.NoWrap; |
| 125 | StringFormat l = new StringFormat(c); l.Alignment = StringAlignment.Near; |
| 126 | StringFormat r = new StringFormat(c); r.Alignment = StringAlignment.Far; |
| 127 | int y = Theme.S(12); |
| 128 | using (SolidBrush o = new SolidBrush(Theme.Orange)) g.DrawString(Theme.Spaced("NOW PLAYING"), head, o, new RectangleF(pad, y, W - pad * 2, Theme.S(18)), l); |
| 129 | y += Theme.S(24); |
| 130 | |
| 131 | bool any = Has || Found; |
| 132 | using (SolidBrush jb = new SolidBrush(any ? Theme.Orange : Theme.Dim)) |
| 133 | g.DrawString(any ? "Jamming? Control your media here!" : "No Spotify or YouTube Music Desktop open right now.", any ? jam : small, jb, new RectangleF(pad, y, W - pad * 2, Theme.S(36)), c); |
| 134 | y += Theme.S(40); |
| 135 | |
| 136 | int ts = Math.Min(W - pad * 2, Theme.S(190)); |
| 137 | Rectangle tr = new Rectangle((W - ts) / 2, y, ts, ts); |
| 138 | using (GraphicsPath tp = Round(tr, Theme.S(8))) |
| 139 | { |
| 140 | if (Thumb != null) |
| 141 | { |
| 142 | GraphicsState st = g.Save(); |
| 143 | g.SetClip(tp); |
| 144 | g.InterpolationMode = InterpolationMode.HighQualityBicubic; |
| 145 | float sc = Math.Max(ts / (float)Thumb.Width, ts / (float)Thumb.Height); |
| 146 | int sw = (int)(ts / sc), sh = (int)(ts / sc); |
| 147 | g.DrawImage(Thumb, tr, new Rectangle((Thumb.Width - sw) / 2, (Thumb.Height - sh) / 2, sw, sh), GraphicsUnit.Pixel); |
| 148 | g.Restore(st); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | using (SolidBrush sb = new SolidBrush(Theme.Sunk)) g.FillPath(sb, tp); |
| 153 | StringFormat m = new StringFormat(); m.Alignment = StringAlignment.Center; m.LineAlignment = StringAlignment.Center; |
| 154 | using (SolidBrush db = new SolidBrush(Theme.Faint)) |
| 155 | if (iconsBig != null) using (Font f = new Font("Segoe MDL2 Assets", 40f * Theme.Scale)) g.DrawString("", f, db, tr, m); |
| 156 | } |
| 157 | } |
| 158 | y += ts + Theme.S(14); |
| 159 | |
| 160 | string t = Has ? Title : FallbackTitle; |
| 161 | string a = Has ? Artist : ""; |
| 162 | using (SolidBrush tb = new SolidBrush(Theme.Bright)) g.DrawString(t.Length > 0 ? t : (any ? "Nothing playing" : ""), title, tb, new RectangleF(pad, y, W - pad * 2, Theme.S(26)), c); |
| 163 | y += Theme.S(26); |
| 164 | using (SolidBrush ab = new SolidBrush(Theme.Soft)) g.DrawString(a, artist, ab, new RectangleF(pad, y, W - pad * 2, Theme.S(20)), c); |
| 165 | y += Theme.S(20); |
| 166 | string app = Has ? App : FallbackApp; |
| 167 | using (SolidBrush db = new SolidBrush(Theme.Dim)) g.DrawString(app.Length > 0 ? (Album.Length > 0 && Has ? Album + " · " + app : app) : "", small, db, new RectangleF(pad, y, W - pad * 2, Theme.S(18)), c); |
| 168 | y += Theme.S(26); |
| 169 | |
| 170 | TimeSpan pos = LivePos(); |
| 171 | double frac = End > TimeSpan.Zero ? Math.Max(0, Math.Min(1, pos.TotalSeconds / End.TotalSeconds)) : 0; |
| 172 | Rectangle br = new Rectangle(pad, y, W - pad * 2, Theme.S(4)); |
| 173 | bar = new Rectangle(br.X, br.Y - Theme.S(8), br.Width, br.Height + Theme.S(16)); |
| 174 | using (GraphicsPath bp = Round(br, Theme.S(2))) using (SolidBrush lb = new SolidBrush(Theme.Line)) g.FillPath(lb, bp); |
| 175 | int fw = (int)(br.Width * frac); |
| 176 | if (fw > 1) using (GraphicsPath fp = Round(new Rectangle(br.X, br.Y, fw, br.Height), Theme.S(2))) using (SolidBrush ob = new SolidBrush(hoverBar && CanSeek ? Theme.Orange : Theme.Text)) g.FillPath(ob, fp); |
| 177 | if (Has && End > TimeSpan.Zero) |
| 178 | using (SolidBrush kb = new SolidBrush(Theme.Bright)) g.FillEllipse(kb, br.X + fw - Theme.S(6), br.Y + br.Height / 2 - Theme.S(6), Theme.S(12), Theme.S(12)); |
| 179 | y += Theme.S(10); |
| 180 | using (SolidBrush db = new SolidBrush(Theme.Soft)) |
| 181 | { |
| 182 | g.DrawString(Has ? Clock(pos) : "", small, db, new RectangleF(pad, y, W / 2 - pad, Theme.S(18)), l); |
| 183 | g.DrawString(Has && End > TimeSpan.Zero ? Clock(End) : "", small, db, new RectangleF(W / 2, y, W / 2 - pad, Theme.S(18)), r); |
| 184 | } |
| 185 | y += Theme.S(26); |
| 186 | |
| 187 | int bs = Theme.S(40), big = Theme.S(56); |
| 188 | int gap = Math.Max(Theme.S(4), (W - pad * 2 - bs * 4 - big) / 4); |
| 189 | int x = (W - (bs * 4 + big + gap * 4)) / 2; |
| 190 | int cy = y + big / 2; |
| 191 | bool live = Has || Found; |
| 192 | Glyph(g, "shuffle", '', "Shuf", new Rectangle(x, cy - bs / 2, bs, bs), Shuffle, Has && CanShuffle, false); x += bs + gap; |
| 193 | Glyph(g, "prev", '', "Prev", new Rectangle(x, cy - bs / 2, bs, bs), false, live, false); x += bs + gap; |
| 194 | Rectangle pr = new Rectangle(x, cy - big / 2, big, big); |
| 195 | btns["play"] = pr; |
| 196 | using (SolidBrush pb = new SolidBrush(!live ? Theme.Line : hover == "play" ? Theme.Orange : Theme.Bright)) g.FillEllipse(pb, pr); |
| 197 | StringFormat mm = new StringFormat(); mm.Alignment = StringAlignment.Center; mm.LineAlignment = StringAlignment.Center; |
| 198 | using (SolidBrush ib = new SolidBrush(Theme.Back)) |
| 199 | { |
| 200 | if (iconsBig != null) g.DrawString(Playing ? "" : "", iconsBig, ib, pr, mm); |
| 201 | else g.DrawString(Playing ? "II" : ">", jam, ib, pr, mm); |
| 202 | } |
| 203 | x += big + gap; |
| 204 | Glyph(g, "next", '', "Next", new Rectangle(x, cy - bs / 2, bs, bs), false, live, false); x += bs + gap; |
| 205 | Glyph(g, "repeat", Repeat == 1 ? '' : '', "Rep", new Rectangle(x, cy - bs / 2, bs, bs), Repeat != 0, Has && CanRepeat, false); |
| 206 | y += big + Theme.S(14); |
| 207 | |
| 208 | int vb = Theme.S(34); |
| 209 | int vx = (W - vb * 3 - Theme.S(8) * 2) / 2; |
| 210 | Glyph(g, "voldown", '', "Vol-", new Rectangle(vx, y, vb, vb), false, true, false); vx += vb + Theme.S(8); |
| 211 | Glyph(g, "mute", '', "Mute", new Rectangle(vx, y, vb, vb), false, true, false); vx += vb + Theme.S(8); |
| 212 | Glyph(g, "volup", '', "Vol+", new Rectangle(vx, y, vb, vb), false, true, false); |
| 213 | y += vb + Theme.S(6); |
| 214 | using (SolidBrush fb = new SolidBrush(Theme.Faint)) g.DrawString("Volume is your PC's volume", small, fb, new RectangleF(pad, y, W - pad * 2, Theme.S(18)), c); |
| 215 | } |
| 216 | |
| 217 | public int Wanted() |
| 218 | { |
| 219 | return Theme.S(12 + 24 + 40) + Math.Min(Width - Theme.S(32), Theme.S(190)) + Theme.S(14 + 26 + 20 + 26 + 10 + 26 + 56 + 14 + 34 + 6 + 26); |
| 220 | } |
| 221 | |
| 222 | protected override void OnMouseMove(MouseEventArgs e) |
| 223 | { |
| 224 | string h = null; |
| 225 | foreach (KeyValuePair<string, Rectangle> kv in btns) if (kv.Value.Contains(e.Location)) h = kv.Key; |
| 226 | bool hb = Has && CanSeek && bar.Contains(e.Location); |
| 227 | if (h != hover || hb != hoverBar) { hover = h; hoverBar = hb; Invalidate(); } |
| 228 | Cursor = h != null || hb ? Cursors.Hand : Cursors.Default; |
| 229 | } |
| 230 | |
| 231 | protected override void OnMouseLeave(EventArgs e) |
| 232 | { |
| 233 | hover = null; |
| 234 | hoverBar = false; |
| 235 | Invalidate(); |
| 236 | } |
| 237 | |
| 238 | protected override void OnMouseDown(MouseEventArgs e) |
| 239 | { |
| 240 | if (e.Button != MouseButtons.Left) return; |
| 241 | if (Has && CanSeek && bar.Contains(e.Location) && bar.Width > 0) |
| 242 | { |
| 243 | if (Seek != null) Seek(Math.Max(0, Math.Min(1, (e.X - bar.X) / (double)bar.Width))); |
| 244 | return; |
| 245 | } |
| 246 | foreach (KeyValuePair<string, Rectangle> kv in btns) |
| 247 | if (kv.Value.Contains(e.Location) && Command != null) { Command(kv.Key); return; } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | internal partial class MainForm |
| 252 | { |
| 253 | private readonly NowPlayingCard np = new NowPlayingCard(); |
| 254 | private readonly System.Windows.Forms.Timer npTimer = new System.Windows.Forms.Timer(); |
| 255 | private GlobalSystemMediaTransportControlsSessionManager smtc; |
| 256 | private GlobalSystemMediaTransportControlsSession smtcSession; |
| 257 | private bool smtcBusy; |
| 258 | private int smtcFails, npTicks; |
| 259 | private string thumbKey; |
| 260 | |
| 261 | private void SetupNowPlaying() |
| 262 | { |
| 263 | np.Visible = false; |
| 264 | np.Command += MediaCommand; |
| 265 | np.Seek += MediaSeek; |
| 266 | view.Controls.Add(np); |
| 267 | SetupLyrics(); |
| 268 | view.Resize += delegate { PlaceNowPlaying(); }; |
| 269 | npTimer.Interval = 250; |
| 270 | npTimer.Tick += delegate |
| 271 | { |
| 272 | if (!np.Visible) return; |
| 273 | np.Invalidate(); |
| 274 | lyr.Invalidate(); |
| 275 | if (++npTicks % 4 == 0) { PollMedia(); LyricsCheck(); } |
| 276 | }; |
| 277 | npTimer.Start(); |
| 278 | } |
| 279 | |
| 280 | private void PlaceNowPlaying() |
| 281 | { |
| 282 | Rectangle r = view.ColumnBounds(0); |
| 283 | np.Bounds = new Rectangle(r.X, r.Y, r.Width, Math.Min(r.Height, np.Wanted())); |
| 284 | int ly = np.Bottom + Theme.S(12); |
| 285 | lyr.Bounds = new Rectangle(r.X, ly, r.Width, Math.Max(Theme.S(220), r.Bottom - ly)); |
| 286 | } |
| 287 | |
| 288 | private void ShowNowPlaying(bool on) |
| 289 | { |
| 290 | np.Visible = on; |
| 291 | lyr.Visible = on; |
| 292 | if (on) { PlaceNowPlaying(); PollMedia(); LyricsCheck(); } |
| 293 | } |
| 294 | |
| 295 | private static string AppOf(string id) |
| 296 | { |
| 297 | string l = (id ?? "").ToLowerInvariant(); |
| 298 | if (l.Contains("spotify")) return "Spotify"; |
| 299 | if (l.Contains("youtube") || l.Contains("ytm")) return "YouTube Music Desktop"; |
| 300 | return null; |
| 301 | } |
| 302 | |
| 303 | private async void PollMedia() |
| 304 | { |
| 305 | if (smtcBusy || smtcFails > 5) return; |
| 306 | smtcBusy = true; |
| 307 | try |
| 308 | { |
| 309 | if (smtc == null) smtc = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync().AsTask(); |
| 310 | GlobalSystemMediaTransportControlsSession pick = null; |
| 311 | int best = -1; |
| 312 | foreach (GlobalSystemMediaTransportControlsSession s in smtc.GetSessions()) |
| 313 | { |
| 314 | if (AppOf(s.SourceAppUserModelId) == null) continue; |
| 315 | int score = s.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing ? 2 : 1; |
| 316 | if (score > best) { best = score; pick = s; } |
| 317 | } |
| 318 | smtcSession = pick; |
| 319 | if (pick == null) { np.Clear(); thumbKey = null; } |
| 320 | else |
| 321 | { |
| 322 | GlobalSystemMediaTransportControlsSessionMediaProperties pr = await pick.TryGetMediaPropertiesAsync().AsTask(); |
| 323 | GlobalSystemMediaTransportControlsSessionTimelineProperties tl = pick.GetTimelineProperties(); |
| 324 | GlobalSystemMediaTransportControlsSessionPlaybackInfo pb = pick.GetPlaybackInfo(); |
| 325 | np.Has = true; |
| 326 | np.App = AppOf(pick.SourceAppUserModelId); |
| 327 | np.Title = pr.Title ?? ""; |
| 328 | np.Artist = pr.Artist ?? ""; |
| 329 | np.Album = pr.AlbumTitle ?? ""; |
| 330 | np.Pos = tl.Position - tl.StartTime; |
| 331 | np.End = tl.EndTime - tl.StartTime; |
| 332 | np.Stamp = tl.LastUpdatedTime; |
| 333 | np.Playing = pb.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing; |
| 334 | np.Shuffle = pb.IsShuffleActive.HasValue && pb.IsShuffleActive.Value; |
| 335 | np.Repeat = pb.AutoRepeatMode.HasValue ? (int)pb.AutoRepeatMode.Value : 0; |
| 336 | np.CanShuffle = pb.Controls.IsShuffleEnabled; |
| 337 | np.CanRepeat = pb.Controls.IsRepeatEnabled; |
| 338 | np.CanSeek = pb.Controls.IsPlaybackPositionEnabled; |
| 339 | string key = np.Title + "\u0001" + np.Artist + "\u0001" + np.Album; |
| 340 | if (key != thumbKey) |
| 341 | { |
| 342 | thumbKey = key; |
| 343 | Image img = null; |
| 344 | if (pr.Thumbnail != null) |
| 345 | { |
| 346 | try |
| 347 | { |
| 348 | Windows.Storage.Streams.IRandomAccessStreamWithContentType st = await pr.Thumbnail.OpenReadAsync().AsTask(); |
| 349 | using (Stream s = st.AsStreamForRead()) |
| 350 | using (MemoryStream ms = new MemoryStream()) |
| 351 | { |
| 352 | s.CopyTo(ms); |
| 353 | ms.Position = 0; |
| 354 | using (Image raw = Image.FromStream(ms)) img = new Bitmap(raw); |
| 355 | } |
| 356 | } |
| 357 | catch (Exception) { img = null; } |
| 358 | } |
| 359 | np.SetThumb(img); |
| 360 | } |
| 361 | if (!np.Found && !jamShown) |
| 362 | { |
| 363 | jamShown = true; |
| 364 | SetMessage("Jamming? Control your media here! (Music tab)", false); |
| 365 | } |
| 366 | } |
| 367 | smtcFails = 0; |
| 368 | } |
| 369 | catch (Exception) { smtcFails++; smtc = null; } |
| 370 | finally { smtcBusy = false; np.Invalidate(); } |
| 371 | } |
| 372 | |
| 373 | private async void MediaCommand(string c) |
| 374 | { |
| 375 | GlobalSystemMediaTransportControlsSession s = smtcSession; |
| 376 | try |
| 377 | { |
| 378 | switch (c) |
| 379 | { |
| 380 | case "play": if (s != null) await s.TryTogglePlayPauseAsync().AsTask(); else MediaKey(179); break; |
| 381 | case "next": if (s != null) await s.TrySkipNextAsync().AsTask(); else MediaKey(176); break; |
| 382 | case "prev": if (s != null) await s.TrySkipPreviousAsync().AsTask(); else MediaKey(177); break; |
| 383 | case "shuffle": if (s != null) await s.TryChangeShuffleActiveAsync(!np.Shuffle).AsTask(); break; |
| 384 | case "repeat": |
| 385 | if (s != null) |
| 386 | { |
| 387 | MediaPlaybackAutoRepeatMode next = np.Repeat == 0 ? MediaPlaybackAutoRepeatMode.List : np.Repeat == 2 ? MediaPlaybackAutoRepeatMode.Track : MediaPlaybackAutoRepeatMode.None; |
| 388 | await s.TryChangeAutoRepeatModeAsync(next).AsTask(); |
| 389 | } |
| 390 | break; |
| 391 | case "voldown": MediaKey(174); break; |
| 392 | case "volup": MediaKey(175); break; |
| 393 | case "mute": MediaKey(173); break; |
| 394 | } |
| 395 | } |
| 396 | catch (Exception) { } |
| 397 | npTicks = 3; |
| 398 | PollMedia(); |
| 399 | } |
| 400 | |
| 401 | private async void MediaSeek(double f) |
| 402 | { |
| 403 | GlobalSystemMediaTransportControlsSession s = smtcSession; |
| 404 | if (s == null || np.End <= TimeSpan.Zero) return; |
| 405 | try |
| 406 | { |
| 407 | TimeSpan to = TimeSpan.FromTicks((long)(np.End.Ticks * f)); |
| 408 | np.Pos = to; |
| 409 | np.Stamp = DateTimeOffset.Now; |
| 410 | np.Invalidate(); |
| 411 | await s.TryChangePlaybackPositionAsync(to.Ticks).AsTask(); |
| 412 | } |
| 413 | catch (Exception) { } |
| 414 | PollMedia(); |
| 415 | } |
| 416 | |
| 417 | private static void MediaKey(byte vk) |
| 418 | { |
| 419 | keybd_event(vk, 0, 1, UIntPtr.Zero); |
| 420 | keybd_event(vk, 0, 3, UIntPtr.Zero); |
| 421 | } |
| 422 | } |
| 423 | } |