Files
MediaControl/Program.cs
2025-04-05 17:47:26 -07:00

57 lines
2.0 KiB
C#

using System;
using Windows.Media.Control;
using WindowsMediaController;
namespace MediaControl
{
class Program
{
static void Main(string[] args)
{
// We expect one argument: playpause, next, or previous
if (args.Length == 0)
{
Console.WriteLine("Usage: MediaControl.exe <playpause|next|previous>");
return;
}
var command = args[0].ToLowerInvariant();
using (var mediaManager = new MediaManager())
{
mediaManager.Start();
// MediaManager.CurrentMediaSessions is a Dictionary<string, MediaSession>
var sessions = mediaManager.CurrentMediaSessions;
foreach (var sessionKvp in sessions)
{
var session = sessionKvp.Value;
// Filter so only sessions with "spotify" in the Id get commands
// You might need to do a more exact match depending on the session's actual Id:
// e.g., if (session.Id?.Equals("Spotify.exe", StringComparison.OrdinalIgnoreCase) == true)
if (!session.Id?.ToLower().Contains("spotify") == true)
continue; // skip non-Spotify sessions
var controlSession = session.ControlSession;
if (controlSession == null)
continue;
switch (command)
{
case "playpause":
controlSession.TryTogglePlayPauseAsync();
break;
case "next":
controlSession.TrySkipNextAsync();
break;
case "previous":
controlSession.TrySkipPreviousAsync();
break;
}
}
}
}
}
}