Compare commits
No commits in common. "master" and "1.0" have entirely different histories.
@ -2,14 +2,13 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CS2GSI" Version="1.0.7" />
|
<PackageReference Include="CS2GSI" Version="1.0.7" />
|
||||||
<PackageReference Include="Dubya.WindowsMediaController" Version="2.5.3" />
|
|
||||||
<PackageReference Include="GlaxLogger" Version="1.0.6" />
|
<PackageReference Include="GlaxLogger" Version="1.0.6" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
33
CSMediaControl/MediaController.cs
Normal file
33
CSMediaControl/MediaController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,11 @@
|
|||||||
// See https://aka.ms/new-console-template for more information
|
// See https://aka.ms/new-console-template for more information
|
||||||
|
|
||||||
using CSMediaControl;
|
using CSMediaControl;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
int logLevel = Array.IndexOf(args, "-l") + 1;
|
ConsoleKey key;
|
||||||
LogLevel? level = logLevel >= 0 && logLevel < args.Length ? Enum.Parse<LogLevel>(args[logLevel]) : null;
|
do
|
||||||
Watchdog _ = new (level);
|
{
|
||||||
|
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);
|
@ -1,8 +1,6 @@
|
|||||||
using Windows.Media.Control;
|
using CS2GSI;
|
||||||
using CS2GSI;
|
|
||||||
using GlaxLogger;
|
using GlaxLogger;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using WindowsMediaController;
|
|
||||||
|
|
||||||
namespace CSMediaControl;
|
namespace CSMediaControl;
|
||||||
using GSI = CS2GSI.CS2GSI;
|
using GSI = CS2GSI.CS2GSI;
|
||||||
@ -12,42 +10,27 @@ public class Watchdog : IDisposable, IAsyncDisposable
|
|||||||
private bool _keepRunning = true;
|
private bool _keepRunning = true;
|
||||||
private readonly GSI _gsi;
|
private readonly GSI _gsi;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
private readonly MediaManager _mediaManager = new();
|
private bool _mediaRunning;
|
||||||
private readonly bool _changeState = true;
|
|
||||||
private List<MediaManager.MediaSession> _pausedSessions = new();
|
|
||||||
|
|
||||||
public Watchdog(LogLevel? logLevel)
|
public Watchdog(bool mediaRunning)
|
||||||
{
|
{
|
||||||
this._logger = new Logger(logLevel ?? LogLevel.Information, consoleOut: Console.Out);
|
this._mediaRunning = mediaRunning;
|
||||||
this._mediaManager.Start();
|
this._logger = new Logger(LogLevel.Information, consoleOut: Console.Out);
|
||||||
this._gsi = new GSI(this._logger);
|
this._gsi = new GSI(this._logger);
|
||||||
|
|
||||||
this._gsi.OnRoundStart += MusicStop;
|
this._gsi.OnRoundStart += MusicStop;
|
||||||
this._gsi.OnRoundOver += MusicStart;
|
this._gsi.OnRoundOver += MusicStart;
|
||||||
this._gsi.OnDeath += 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");
|
Console.WriteLine("Press p to pause/play media.");
|
||||||
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)
|
while (_keepRunning)
|
||||||
{
|
{
|
||||||
if (Console.KeyAvailable)
|
if (Console.KeyAvailable && Console.ReadKey(true).Key is ConsoleKey.P)
|
||||||
{
|
{
|
||||||
switch (Console.ReadKey(true).Key)
|
if(this._mediaRunning)
|
||||||
{
|
MusicStop(null);
|
||||||
case ConsoleKey.P:
|
else
|
||||||
_changeState = !_changeState;
|
MusicStart(null);
|
||||||
this._logger.Log(LogLevel.Information, $"Execution {(_changeState ? "resumed" : "paused")}");
|
|
||||||
break;
|
|
||||||
case ConsoleKey.M:
|
|
||||||
MusicStop(null);
|
|
||||||
break;
|
|
||||||
case ConsoleKey.R:
|
|
||||||
MusicStart(null);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}else
|
}else
|
||||||
Thread.Sleep(10);
|
Thread.Sleep(10);
|
||||||
}
|
}
|
||||||
@ -56,33 +39,21 @@ public class Watchdog : IDisposable, IAsyncDisposable
|
|||||||
|
|
||||||
private void MusicStop(CS2EventArgs? _)
|
private void MusicStop(CS2EventArgs? _)
|
||||||
{
|
{
|
||||||
if (_changeState)
|
if (_mediaRunning)
|
||||||
{
|
{
|
||||||
this._logger.Log(LogLevel.Information, "Music Pause");
|
this._logger.Log(LogLevel.Information, "Music Pause");
|
||||||
MediaManager.MediaSession[] playingSessions =
|
MediaController.PlayPause();
|
||||||
this._mediaManager.CurrentMediaSessions.Values.Where(session =>
|
this._mediaRunning = false;
|
||||||
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? _)
|
private void MusicStart(CS2EventArgs? _)
|
||||||
{
|
{
|
||||||
if (_changeState && _pausedSessions.Any())
|
if (!_mediaRunning)
|
||||||
{
|
{
|
||||||
this._logger.Log(LogLevel.Information, "Music Start");
|
this._logger.Log(LogLevel.Information, "Music Start");
|
||||||
foreach (MediaManager.MediaSession session in _pausedSessions)
|
MediaController.PlayPause();
|
||||||
{
|
this._mediaRunning = true;
|
||||||
session.ControlSession.TryPlayAsync();
|
|
||||||
this._logger.Log(LogLevel.Debug, $"Resuming {session.Id}");
|
|
||||||
}
|
|
||||||
this._pausedSessions.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,14 +61,12 @@ public class Watchdog : IDisposable, IAsyncDisposable
|
|||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
this._keepRunning = false;
|
this._keepRunning = false;
|
||||||
_mediaManager.Dispose();
|
|
||||||
_logger.Dispose();
|
_logger.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
public async ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
this._keepRunning = false;
|
this._keepRunning = false;
|
||||||
_mediaManager.Dispose();
|
|
||||||
await _logger.DisposeAsync();
|
await _logger.DisposeAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user