Compare commits

..

No commits in common. "master" and "1.0" have entirely different histories.
master ... 1.0

4 changed files with 58 additions and 54 deletions

View File

@ -2,14 +2,13 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework>
<TargetFramework>net7.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

@ -0,0 +1,33 @@
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,8 +1,11 @@
// See https://aka.ms/new-console-template for more information
using CSMediaControl;
using Microsoft.Extensions.Logging;
int logLevel = Array.IndexOf(args, "-l") + 1;
LogLevel? level = logLevel >= 0 && logLevel < args.Length ? Enum.Parse<LogLevel>(args[logLevel]) : null;
Watchdog _ = new (level);
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);

View File

@ -1,8 +1,6 @@
using Windows.Media.Control;
using CS2GSI;
using CS2GSI;
using GlaxLogger;
using Microsoft.Extensions.Logging;
using WindowsMediaController;
namespace CSMediaControl;
using GSI = CS2GSI.CS2GSI;
@ -12,42 +10,27 @@ public class Watchdog : IDisposable, IAsyncDisposable
private bool _keepRunning = true;
private readonly GSI _gsi;
private readonly Logger _logger;
private readonly MediaManager _mediaManager = new();
private readonly bool _changeState = true;
private List<MediaManager.MediaSession> _pausedSessions = new();
private bool _mediaRunning;
public Watchdog(LogLevel? logLevel)
public Watchdog(bool mediaRunning)
{
this._logger = new Logger(logLevel ?? LogLevel.Information, consoleOut: Console.Out);
this._mediaManager.Start();
this._mediaRunning = mediaRunning;
this._logger = new Logger(LogLevel.Information, consoleOut: Console.Out);
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;
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");
Console.WriteLine("Press p to pause/play media.");
while (_keepRunning)
{
if (Console.KeyAvailable)
if (Console.KeyAvailable && Console.ReadKey(true).Key is ConsoleKey.P)
{
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;
}
if(this._mediaRunning)
MusicStop(null);
else
MusicStart(null);
}else
Thread.Sleep(10);
}
@ -56,33 +39,21 @@ public class Watchdog : IDisposable, IAsyncDisposable
private void MusicStop(CS2EventArgs? _)
{
if (_changeState)
if (_mediaRunning)
{
this._logger.Log(LogLevel.Information, "Music Pause");
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();
MediaController.PlayPause();
this._mediaRunning = false;
}
}
private void MusicStart(CS2EventArgs? _)
{
if (_changeState && _pausedSessions.Any())
if (!_mediaRunning)
{
this._logger.Log(LogLevel.Information, "Music Start");
foreach (MediaManager.MediaSession session in _pausedSessions)
{
session.ControlSession.TryPlayAsync();
this._logger.Log(LogLevel.Debug, $"Resuming {session.Id}");
}
this._pausedSessions.Clear();
MediaController.PlayPause();
this._mediaRunning = true;
}
}
@ -90,14 +61,12 @@ 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();
}
}