WindowsMediaController

repository·master·Indexed 41 days ago

https://github.com/dubyadude/windowsmediacontroller

A C# wrapper for the Windows System Media Transport Controls (SMTC) API for Windows 10 and 11. It allows developers to monitor and control media sessions, such as Spotify or Chrome, using the MediaManager and MediaSession classes. Version 2.5.6 is available via NuGet.

Tokens
1.7K
Snippets
6
Records
7
Agent score
2%

What's inside WindowsMediaController

  1. How MediaManager and MediaSession work together

    master

    The library uses a hierarchical structure to manage media sessions:

    1. MediaManager: The top-level controller. It manages the global WindowsSessionManager and provides events for any session opening, closing, or changing state across the entire OS.
    2. MediaManager.MediaSession: Represents an individual media session (e.g., a specific Spotify instance or Chrome tab). It contains a ControlSession which provides direct access to that specific session's properties and playback state.

    Use MediaManager.GetFocusedSession() to retrieve the session currently controlling audio.

  2. Initialize and start the MediaManager

    master

    To begin interacting with media sessions, instantiate a MediaManager, subscribe to the desired events, and call Start() or StartAsync().

    mediaManager = new MediaManager();
    
    // Subscribe to events
    mediaManager.OnAnySessionOpened += MediaManager_OnAnySessionOpened;
    mediaManager.OnAnySessionClosed += MediaManager_OnAnySessionClosed;
    mediaManager.OnFocusedSessionChanged += MediaManager_OnFocusedSessionChanged;
    mediaManager.OnAnyMediaPropertyChanged += MediaManager_OnAnyMediaPropertyChanged;
    mediaManager.OnAnyPlaybackStateChanged += MediaManager_OnAnyPlaybackStateChanged;
    mediaManager.OnAnyTimelinePropertyChanged += MediaManager_OnAnyTimelinePropertyChanged;
    
    // Start the manager
    mediaManager.Start();
    // OR
    await mediaManager.StartAsync();
  3. Install Dubya.WindowsMediaController

    master

    You can install the package via NuGet. Depending on your project type, you may need specific configuration to ensure compatibility with Windows Runtime (WinRT).

    <ItemGroup>
      <PackageReference Include="Dubya.WindowsMediaController">
        <Version>2.5.6</Version>
      </PackageReference>
    </ItemGroup>
  4. Configure TargetFramework for .NET 5+

    master

    For .NET 5+ projects, you must modify your TargetFramework in the .csproj file to include the Windows SDK version to allow proper WinRT access. Replace net6.0 with your specific .NET version (e.g., net7.0, net8.0).

    <TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
  5. Get media properties and playback info from a MediaSession

    master

    Once you have a MediaSession instance, you can use its ControlSession to extract specific media data.

    • Playback Info: Use GetPlaybackInfo() to see available actions (play/pause) and current playback state.
    • Media Properties: Use await TryGetMediaPropertiesAsync() to get metadata like song title, author, and thumbnails.
    // Get playback state and available controls
    var playbackInfo = mediaSession.ControlSession.GetPlaybackInfo();
    
    // Get metadata (Title, Author, Thumbnail, etc.)
    var mediaProperties = await mediaSession.ControlSession.TryGetMediaPropertiesAsync();
  6. MediaManager API Reference

    master

    The MediaManager class is the primary entry point for the library.

    // Properties
    ReadOnlyDictionary<string, MediaSession> CurrentMediaSessions;
    bool IsStarted { get; }
    GlobalSystemMediaTransportControlsSessionManager WindowsSessionManager;
    
    // Methods
    void Start();
    async Task StartAsync();
    MediaSession GetFocusedSession();
    void ForceUpdate();
    
    // Events
    void OnAnySessionOpened(MediaManager.MediaSession session);
    void OnAnySessionClosed(MediaManager.MediaSession session);
    void OnFocusedSessionChanged(MediaManager.MediaSession session);
    void OnAnyMediaPropertyChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionMediaProperties args);
    void OnAnyPlaybackStateChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionPlaybackInfo args);
    void OnAnyTimelinePropertyChanged(MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionTimelineProperties timelineProperties);
  7. MediaSession API Reference

    master

    The MediaSession class represents a specific media source.

    // Properties
    readonly string Id;
    GlobalSystemMediaTransportControlsSession ControlSession { get; }
    
    // Events
    void OnSessionClosed(MediaManager.MediaSession session);
    void OnMediaPropertyChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionMediaProperties args);
    void OnPlaybackStateChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionPlaybackInfo args);
    void OnTimelinePropertyChanged(MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionTimelineProperties timelineProperties);