From d611f6fa1a279b67b00f698be493ca819b37fb19 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 22 Feb 2024 00:37:44 +0100 Subject: [PATCH] I love https://github.com/DubyaDude/WindowsMediaController --- CSMediaControl/CSMediaControl.csproj | 3 +- CSMediaControl/MediaController.cs | 33 -------------- CSMediaControl/Program.cs | 11 ++--- CSMediaControl/Watchdog.cs | 65 ++++++++++++++++++++-------- 4 files changed, 54 insertions(+), 58 deletions(-) delete mode 100644 CSMediaControl/MediaController.cs diff --git a/CSMediaControl/CSMediaControl.csproj b/CSMediaControl/CSMediaControl.csproj index fd7d1ce..4410904 100644 --- a/CSMediaControl/CSMediaControl.csproj +++ b/CSMediaControl/CSMediaControl.csproj @@ -2,13 +2,14 @@ Exe - net7.0 + net7.0-windows10.0.22000.0 enable enable + diff --git a/CSMediaControl/MediaController.cs b/CSMediaControl/MediaController.cs deleted file mode 100644 index 36d7131..0000000 --- a/CSMediaControl/MediaController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Runtime.InteropServices; - -namespace CSMediaControl; - -internal static class MediaController -{ - [DllImport("user32.dll")] - // ReSharper disable once IdentifierTypo - private static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo); - private const int KeyEventFExtendedKey = 1; - //private const int KeyEventFKeyup = 0; - //https://learn.microsoft.com/de-de/windows/win32/inputdev/virtual-key-codes - - public static void PlayPause() - { - keybd_event(0xB3, 0, KeyEventFExtendedKey, IntPtr.Zero); - } - - public static void Next() - { - keybd_event(0xB0, 0, KeyEventFExtendedKey, IntPtr.Zero); - } - - public static void Previous() - { - keybd_event(0xB1, 0, KeyEventFExtendedKey, IntPtr.Zero); - } - - public static void Stop() - { - keybd_event(0xB2, 0, KeyEventFExtendedKey, IntPtr.Zero); - } -} \ No newline at end of file diff --git a/CSMediaControl/Program.cs b/CSMediaControl/Program.cs index 09292d0..d3a8625 100644 --- a/CSMediaControl/Program.cs +++ b/CSMediaControl/Program.cs @@ -1,11 +1,8 @@ // See https://aka.ms/new-console-template for more information using CSMediaControl; +using Microsoft.Extensions.Logging; -ConsoleKey key; -do -{ - Console.WriteLine("Is media currently playing? (y/n)"); - key = Console.ReadKey(true).Key; -} while (key != ConsoleKey.Y && key != ConsoleKey.N); -Watchdog _ = new (key == ConsoleKey.Y); \ No newline at end of file +int logLevel = Array.IndexOf(args, "-l") + 1; +LogLevel? level = logLevel >= 0 && logLevel < args.Length ? Enum.Parse(args[logLevel]) : null; +Watchdog _ = new (level); \ No newline at end of file diff --git a/CSMediaControl/Watchdog.cs b/CSMediaControl/Watchdog.cs index b27033b..a1110f4 100644 --- a/CSMediaControl/Watchdog.cs +++ b/CSMediaControl/Watchdog.cs @@ -1,6 +1,8 @@ -using CS2GSI; +using Windows.Media.Control; +using CS2GSI; using GlaxLogger; using Microsoft.Extensions.Logging; +using WindowsMediaController; namespace CSMediaControl; using GSI = CS2GSI.CS2GSI; @@ -10,27 +12,42 @@ public class Watchdog : IDisposable, IAsyncDisposable private bool _keepRunning = true; private readonly GSI _gsi; private readonly Logger _logger; - private bool _mediaRunning; + private readonly MediaManager _mediaManager = new(); + private readonly bool _changeState = true; + private List _pausedSessions = new(); - public Watchdog(bool mediaRunning) + public Watchdog(LogLevel? logLevel) { - this._mediaRunning = mediaRunning; - this._logger = new Logger(LogLevel.Information, consoleOut: Console.Out); + this._logger = new Logger(logLevel ?? LogLevel.Information, consoleOut: Console.Out); + this._mediaManager.Start(); this._gsi = new GSI(this._logger); this._gsi.OnRoundStart += MusicStop; this._gsi.OnRoundOver += MusicStart; this._gsi.OnDeath += MusicStart; + this._gsi.OnHalfTime += MusicStart; + this._gsi.OnMatchOver += MusicStart; - Console.WriteLine("Press p to pause/play media."); + this._logger.Log(LogLevel.None, "To pause/resume the program, press P"); + this._logger.Log(LogLevel.None, "To pause all media, press m"); + this._logger.Log(LogLevel.None, "To resume all paused media, press r"); while (_keepRunning) { - if (Console.KeyAvailable && Console.ReadKey(true).Key is ConsoleKey.P) + if (Console.KeyAvailable) { - if(this._mediaRunning) - MusicStop(null); - else - MusicStart(null); + switch (Console.ReadKey(true).Key) + { + case ConsoleKey.P: + _changeState = !_changeState; + this._logger.Log(LogLevel.Information, $"Execution {(_changeState ? "resumed" : "paused")}"); + break; + case ConsoleKey.M: + MusicStop(null); + break; + case ConsoleKey.R: + MusicStart(null); + break; + } }else Thread.Sleep(10); } @@ -39,21 +56,33 @@ public class Watchdog : IDisposable, IAsyncDisposable private void MusicStop(CS2EventArgs? _) { - if (_mediaRunning) + if (_changeState) { this._logger.Log(LogLevel.Information, "Music Pause"); - MediaController.PlayPause(); - this._mediaRunning = false; + MediaManager.MediaSession[] playingSessions = + this._mediaManager.CurrentMediaSessions.Values.Where(session => + session.ControlSession.GetPlaybackInfo().PlaybackStatus == + GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing).ToArray(); + foreach (MediaManager.MediaSession session in playingSessions) + { + this._logger.Log(LogLevel.Debug, $"Pausing {session.Id}"); + session.ControlSession.TryPauseAsync(); + } + this._pausedSessions = _pausedSessions.Concat(playingSessions).ToList(); } } private void MusicStart(CS2EventArgs? _) { - if (!_mediaRunning) + if (_changeState && _pausedSessions.Any()) { this._logger.Log(LogLevel.Information, "Music Start"); - MediaController.PlayPause(); - this._mediaRunning = true; + foreach (MediaManager.MediaSession session in _pausedSessions) + { + session.ControlSession.TryPlayAsync(); + this._logger.Log(LogLevel.Debug, $"Resuming {session.Id}"); + } + this._pausedSessions.Clear(); } } @@ -61,12 +90,14 @@ public class Watchdog : IDisposable, IAsyncDisposable public void Dispose() { this._keepRunning = false; + _mediaManager.Dispose(); _logger.Dispose(); } public async ValueTask DisposeAsync() { this._keepRunning = false; + _mediaManager.Dispose(); await _logger.DisposeAsync(); } } \ No newline at end of file