glax 2024-02-22 00:37:44 +01:00
parent 7a8cfd315c
commit d611f6fa1a
4 changed files with 54 additions and 58 deletions

View File

@ -2,13 +2,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CS2GSI" Version="1.0.7" />
<PackageReference Include="Dubya.WindowsMediaController" Version="2.5.3" />
<PackageReference Include="GlaxLogger" Version="1.0.6" />
</ItemGroup>

View File

@ -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);
}
}

View File

@ -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);
int logLevel = Array.IndexOf(args, "-l") + 1;
LogLevel? level = logLevel >= 0 && logLevel < args.Length ? Enum.Parse<LogLevel>(args[logLevel]) : null;
Watchdog _ = new (level);

View File

@ -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<MediaManager.MediaSession> _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();
}
}