Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

c# - Using SystemMediaTransportControls to get current playing song info from other app

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.imgur.com/nNy16Gs.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 'PackageManagementDefault 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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...