DubyaDude / WindowsMediaController

Allows developers to more easily get information from and interact with the Windows 10/11 OS media interface. (Also referred to Windows System Media Transport Controls (SMTC))
https://nuget.org/packages/Dubya.WindowsMediaController
MIT License
129 stars 9 forks source link

MTC SessionsChanged and CurrentSessionChanged (FocusedSession) not triggering #6

Open Aviv-Frenkel opened 2 years ago

Aviv-Frenkel commented 2 years ago

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

DubyaDude commented 2 years ago

Just to clarify:

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.

Aviv-Frenkel commented 2 years ago

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 commented 2 years ago

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?

DubyaDude commented 2 years ago

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

DubyaDude commented 2 years ago

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?

Aviv-Frenkel commented 2 years ago

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.

Aytackydln commented 2 years ago

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

DubyaDude commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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?

Aytackydln commented 1 year ago

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

DubyaDude commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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