3

I write a some program that should work with another player and retrieve info about current playing song. That player is written using UWP, so Windows knows what track is playing, so i can see it's name and other info when changing volume:

https://i.stack.imgur.com/WSmwh.png

Things I tried:

var systemMediaControls = SystemMediaTransportControls.GetForCurrentView();

From Get current playing track info from Microsoft Groove Music app

Unfortunately, as I understand, it's just for local media, playing from my app.

Background media player doesn't helped too because of same reason.

Is it possible at all to get it from Windows? Or I should directly read memory of that player, heh?

2 Answers2

12

How to display artist and song name:

Get the Microsoft.Windows.SDK.Contracts package if you're not developing a WinRT app.

(Visual Studio) In your NuGet Package Manager settings (Tools->NuGet Package Manager->Package Manager Settings) you need to have 'PackageManagement\Default package management format' set to 'PackageReference'

If you've done that, the package should show up in your References. If that isn't the case, you need to deinstall the package and its dependencies and try again.

Here's example code:

using System;
using System.Threading.Tasks;
using Windows.Media.Control;

public static class Program {
    public static async Task Main(string[] args) {
        var gsmtcsm = await GetSystemMediaTransportControlsSessionManager();
        var mediaProperties = await GetMediaProperties(gsmtcsm.GetCurrentSession());

        Console.WriteLine("{0} - {1}", mediaProperties.Artist, mediaProperties.Title);

        Console.WriteLine("Press any key to quit..");
        Console.ReadKey(true);
    }

    private static async Task<GlobalSystemMediaTransportControlsSessionManager> GetSystemMediaTransportControlsSessionManager() =>
        await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

    private static async Task<GlobalSystemMediaTransportControlsSessionMediaProperties> GetMediaProperties(GlobalSystemMediaTransportControlsSession session) =>
        await session.TryGetMediaPropertiesAsync();
}
Luca Marini
  • 181
  • 2
  • 7
  • If you're still having issues finding the Windows.Media namespace, manually add the reference (using the Add Reference panel in the project menu), browse to "C:\Program Files (x86)\Windows Kits\10\References\", then the folder in there, then Windows.Foundation.UniversalApiContract, then the folder in there, then set the file type selector to all files, and select "Windows.Foundation.UniversalApiContract.winmd" – foxtdev Oct 27 '20 at 00:49
  • could you take a look at this, please. https://stackoverflow.com/questions/64889280/c-sharp-unexpected-time-error-for-globalsystemmediatransportcontrolssessionman –  Nov 20 '20 at 04:35
  • Any Idea how I can subscribe to track changes events like next/prev or play/pause thanks? – Phani Rithvij Jun 15 '21 at 16:19
  • @PhaniRithvij My friend made a library for exactly that, take a look: https://github.com/DubyaDude/WindowsMediaController/blob/master/WindowsMediaController/Main.cs – Luca Marini Jun 16 '21 at 17:29
  • @PhaniRithvij Oh sorry I probably misunderstood you. You can subscribe to the PlaybackInfosChanged event in a GlobalSystemMediaTransportControlsSession object. You can then get the playback info via session.GetPlaybackInfo() and on this object you can put a switch around PlaybackStatus to track those changes. – Luca Marini Jun 16 '21 at 17:44
  • @LucaMarini thank you for replying and providing a reference project, I have one more question if you don't mind, do you know how to do the reverse i.e. act as an audio player application that lets the os (windows) know what's currently playing? I would really appreciate any github repos (c# or c++) which does this as a reference. I've been trying to google this for the last couple of days but only MSDN docs show up. Thank you. – Phani Rithvij Jun 16 '21 at 18:28
  • @PhaniRithvij Take a look here: https://github.com/MicrosoftDocs/windows-uwp/blob/docs/windows-apps-src/audio-video-camera/integrate-with-systemmediatransportcontrols.md – Luca Marini Jun 17 '21 at 17:20
0

The screen control bar displayed by the music application is not controlled by the system but belongs to the application itself.

You don't have a way to get playing music in other music software just as you can't get files from the application folder of other applications.

Anyway, if you just want to get current song from OS, there’s no such API.

Best regards.

Richard Zhang
  • 7,523
  • 1
  • 7
  • 13