WindowsMediaController icon indicating copy to clipboard operation
WindowsMediaController copied to clipboard

MTC SessionsChanged and CurrentSessionChanged (FocusedSession) not triggering

Open Aviv-Frenkel opened this issue 2 years ago • 15 comments

I tried the example project in the repository, I am using Windows 10 Pro 21H2 build 19044.2006.

  • OnAnySessionOpened event is raised only when you launch the program, any new media source can not be detected while the program is running.
  • When you close media source (spotify or chrome for example) the OnAnySessionClosed event is not raised and instead the OnAnyPlaybackStateChanged event is being called multiple times: image If you try to reopen the closed media source, it is not detected neither.

Aviv-Frenkel avatar Oct 15 '22 17:10 Aviv-Frenkel

Just to clarify:

  • Spotify doing multiple pauses is how the media handles its shutdown/startup process. I wish I could tell ya why, but I don't really know.

Currently on Windows 10 Pro 21H2 build 19044.2130 This is currently what I get (added in some lines to show what action me/the user took)

=> Spotify already opened, opened WMC-Sample
[15:41:07.639] -- New Source: Spotify.exe
[15:41:07.647] Spotify.exe is now playing Shake Up Christmas by Train
=> Clicked Play
[15:41:58.742] Spotify.exe is now Playing
=> Clicked Pause
[15:42:11.990] Spotify.exe is now Paused
=> Closed Spotify App (Not playing music) (gotta love this app)
[15:42:25.396] Spotify.exe is now Paused
[15:42:25.397] Spotify.exe is now Paused
[15:42:25.397] Spotify.exe is now Paused
[15:42:25.397] Spotify.exe is now Paused
[15:42:25.398] Spotify.exe is now Paused
[15:42:25.398] Spotify.exe is now Paused
[15:42:25.398] Spotify.exe is now Paused
[15:42:25.399] Spotify.exe is now Paused
[15:42:25.400] Spotify.exe is now playing
[15:42:25.400] Spotify.exe is now Paused
[15:42:25.440] Spotify.exe is now Paused
[15:42:25.440] Spotify.exe is now Paused
[15:42:25.441] Spotify.exe is now Paused
[15:42:25.467] -- Removed Source: Spotify.exe
=> Opened Spotify App (Not playing music) (gotta love this app)
[15:43:00.502] -- New Source: Spotify.exe
[15:43:00.504] Spotify.exe is now playing
[15:43:00.778] Spotify.exe is now playing
[15:43:00.778] Spotify.exe is now Paused
[15:43:00.778] Spotify.exe is now Paused
[15:43:00.779] Spotify.exe is now Paused
[15:43:00.789] Spotify.exe is now playing
[15:43:00.836] Spotify.exe is now playing
[15:43:00.847] Spotify.exe is now playing
[15:43:00.854] Spotify.exe is now Paused
[15:43:00.854] Spotify.exe is now Paused
[15:43:00.854] Spotify.exe is now Paused
[15:43:00.855] Spotify.exe is now Paused
[15:43:00.855] Spotify.exe is now Paused
[15:43:00.855] Spotify.exe is now Paused
[15:43:00.856] Spotify.exe is now Paused
[15:43:00.857] Spotify.exe is now Paused
[15:43:00.862] Spotify.exe is now Paused
[15:43:00.908] Spotify.exe is now Paused
[15:43:00.967] Spotify.exe is now playing Shake Up Christmas by Train
[15:43:00.972] Spotify.exe is now playing Shake Up Christmas by Train

Seems to be working fine, try rebooting/run any pending updates? If that doesn't fix it, I'd be happy to help dig further.

DubyaDude avatar Oct 15 '22 19:10 DubyaDude

As I mentioned above this behavior also occurs for Chrome and other processes. Tomorrow I'll dig into this more to check what is happening. Just want to point out that I tried to use the existing DLL file from the example solution, rebuild the project locally and use the new compiled DLL file and also tried to use the package from the Nuget installer - same behavior with all of these.

Aviv-Frenkel avatar Oct 15 '22 22:10 Aviv-Frenkel

I found out that _WindowsSessionManager.SessionsChanged += SessionsChanged; is never triggered. I also noticed this: image I am using DotNet 4.8, am I missing something?

Aviv-Frenkel avatar Oct 16 '22 16:10 Aviv-Frenkel

Hmmm, can you try changing that line and making it an asyc start?

DubyaDude avatar Oct 16 '22 18:10 DubyaDude

Actually digging more into it, the compiler seemed to be smart enough to replace GetAwaiter().GetResult() to its counterpart

WindowsRuntimeSystemExtensions.GetAwaiter(GlobalSystemMediaTransportControlsSessionManager.RequestAsync()).GetResult()

So generally, that could be ignored.

Rereading this, if SessionsChanged is the only event not being triggered, this may be something that has to do more with the machine itself.

How does the UI sample look when doing these tests?

DubyaDude avatar Oct 16 '22 18:10 DubyaDude

I googled the issue and found this thread: Bug: Media Sessions are not updating in Windows 10 2004 It seems that this is a known bug from a while ago and still present in later Windows versions. Maybe this repository can also help: ADeltaX/NPSMLib. I don't know how the SessionsChanged event triggers on your machine and and not on mine. I'll try to dig in the above repositories and look for their solutions.

Aviv-Frenkel avatar Oct 16 '22 21:10 Aviv-Frenkel

Something I've noticed while I was integrating this library to Artemis and Aurora is: If you don't unsubscribe to the events and close your program the events won't trigger again. Maybe this library needs to use weak events or needs to unsubscribe with dispose methods

Aytackydln avatar Oct 17 '22 21:10 Aytackydln

Something I've noticed while I was integrating this library to Artemis and Aurora is: If you don't unsubscribe to the events and close your program the events won't trigger again. Maybe this library needs to use weak events or needs to unsubscribe with dispose methods

Hmmm, I see, I started running into it now as well, I'll need to test with some reboots to confirm. I'm not too familiar with weak events but sounds interesting. The tool currently does unsubscribe itself via its dispose method here

DubyaDude avatar Oct 17 '22 22:10 DubyaDude

After some testing, The issue goes away for me after logout+login, I haven't been able to purposely trigger it breaking again. I tried creating ~2k instances of media manager and forcefully closing with the dispose method removed. Can't get the issue to recreate itself on demand.

DubyaDude avatar Oct 17 '22 22:10 DubyaDude

Unless you guys think otherwise, I think this is more or less a Windows bug. Not quite sure where/how to report/debug it further.

After some thinking though, a somewhat jank way around this is to have a background task to check if sessions changed by grabbing sessions and comparing them with the local dictionary. Would that be something you'd be interested in?

DubyaDude avatar Oct 28 '22 00:10 DubyaDude

Neither me or any of Aurora's and Artemis plugin users had this issue (reportedly).

I think is is more of a Spotify bug. I've used this library for Chrome, Opera GX a lot and a little for Spotify to test library. Even Chrome and opera GX uses media sessions very differently, Spotify might use media sessions very different too.

If it would help I can share how I use the library: https://github.com/Aytackydln/Artemis.MediaInfo/blob/master/Artemis.MediaInfo/MediaInfoModule.cs

Aytackydln avatar Oct 28 '22 07:10 Aytackydln

I think is is more of a Spotify bug. I've used this library for Chrome, Opera GX a lot and a little for Spotify to test library. Even Chrome and opera GX uses media sessions very differently, Spotify might use media sessions very different too.

If it would help I can share how I use the library: https://github.com/Aytackydln/Artemis.MediaInfo/blob/master/Artemis.MediaInfo/MediaInfoModule.cs

It seems implausible to me that Spotify alone 'breaks' that event entirely. From what I could tell, it happens after a bit of PC uptime. What specific action for me causes it, I'm not sure, but it's been fairly consistent, even on days I don't use Spotify.

Needs some more research in general and I just need to throw more time into figuring out what event actually causes it to break.

DubyaDude avatar Oct 31 '22 03:10 DubyaDude

Also after some more digging, this is a reported issue in Windows Feedback Hub that is marked 'Looking into it'

You need to have the 'Feedback Hub' app to be able to see this (which is slightly annoying). https://aka.ms/AAbz7hd

Screenshot for those that don't want to bother with the feedback hub: image

DubyaDude avatar Oct 31 '22 03:10 DubyaDude

Version 2.3.0 adds a method called ForceUpdate(). This will allow you to do a workaround if desired.

Until Microsoft fixes the issue, this is the best I can really do.

DubyaDude avatar Nov 24 '22 16:11 DubyaDude

Confirmed issue also affects CurrentSessionChanged event (what I named FocusedSession)

DubyaDude avatar Nov 25 '22 17:11 DubyaDude