C# API for displaying currently playing music does not trigger song changed method

  api, c++, windows

I want to get some Information about anything that is currently playing from Windows and show it in a separate window.

Story

Windows does not really have an API for getting Information about something, that is currently playing. So I searched and found this Question here on Stack Overflow: Currently playing song from Windows 10 ‘Now Playing’ card.

Using the API from DubyaDude WindowsMediaController I was able to make this Project: MediaControllUI.

Problem

Using this API, I came up with a strange problem: In his test program, he wrote a method, that is called when the song changes: Program.cs L32-L35

private static void MediaManager_OnSongChanged(MediaManager.MediaSession sender, GlobalSystemMediaTransportControlsSessionMediaProperties args)
{
WriteLineColor($"{sender.ControlSession.SourceAppUserModelId} is now playing {args.Title} {(String.IsNullOrEmpty(args.Artist) ? "" : $"by {args.Artist}")}", ConsoleColor.Cyan);
}

I did exactly the same thing and added the listener like he did:

MediaManager.OnSongChanged += MediaManager_OnSongChanged;
MediaManager.Start();

(As you can see in my MainViewModel and compare it to his Test Program)
His method gets called, whenever the song changes.

It works in his test program, but my method only gets called sometimes. For example if I use Groove-Music, which is only one source, it works fine. But if I use YouTube-Music, I can only get the currently played song on startup and then never again. So, if a new source gets added, it ‘crashes’ (It’s not really crashing, because there is no error I could search for. It just doesn’t work). I tried debugging it and in one out of ten cases it actually works. But it works only in the debugger and only sometimes.

Source: Windows Questions

LEAVE A COMMENT