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

OnFocusedSessionChanged playbackState is Paused when current focused is closed #10

Closed Aytackydln closed 1 year ago

Aytackydln commented 1 year ago

Firstly, thank you for the new event. It really cleans my code for my use case.

The problem is: When the current focused media session is closed, new focused media session's playback state reported as "Paused"

DubyaDude commented 1 year ago

I do not see that on my end, I printed out mediaSession?.ControlSession?.GetPlaybackInfo().PlaybackStatus in the focused change event and got the following:

# Already have Spotify+Firefox opened playing music.
# Starting CMD Sample
[11:46:25.517] -- New Source: Spotify.exe
[11:46:25.529] -- New Source: firefox.exe
[11:46:25.593] Spotify.exe is now playing Tokyo by Dwin
[11:46:25.624] firefox.exe is now playing NieR:Automata OST - Amusement Park (????) Lyric Video by AlpinDale
[11:46:25.625] == Session Focus Changed: firefox.exe, Status: Playing
# Closing Firefox
[11:46:28.459] == Session Focus Changed: Spotify.exe, Status: Playing
[11:46:28.499] -- Removed Source: firefox.exe
# Starting Firefox
[11:46:38.096] -- New Source: firefox.exe
[11:46:38.097] firefox.exe is now playing NieR:Automata OST - Amusement Park (????) Lyric Video by AlpinDale
[11:46:38.097] firefox.exe is now playing NieR:Automata OST - Amusement Park (????) Lyric Video by AlpinDale
[11:46:38.098] firefox.exe is now playing NieR:Automata OST - Amusement Park (????) Lyric Video by AlpinDale
[11:46:38.115] firefox.exe is now playing NieR:Automata OST - Amusement Park (????) Lyric Video by AlpinDale
# Closing Spotify
[11:46:41.807] Spotify.exe is now Paused
[11:46:41.810] Spotify.exe is now Paused
[11:46:41.811] == Session Focus Changed: firefox.exe, Status: Playing
[11:46:41.813] Spotify.exe is now Paused
[11:46:41.814] Spotify.exe is now Paused
[11:46:41.814] Spotify.exe is now Paused
[11:46:41.814] Spotify.exe is now Paused
[11:46:41.814] Spotify.exe is now Paused
[11:46:41.815] Spotify.exe is now Paused
[11:46:41.815] Spotify.exe is now Paused
[11:46:41.815] Spotify.exe is now playing
[11:46:41.816] Spotify.exe is now Paused
[11:46:41.816] Spotify.exe is now Paused
[11:46:41.860] Spotify.exe is now Paused
[11:46:41.860] Spotify.exe is now Paused
[11:46:41.860] Spotify.exe is now Paused
[11:46:41.886] -- Removed Source: Spotify.exe
# Closing Firefox
[11:48:03.808] == Session Focus Changed: , Status:
[11:48:03.809] == Session Focus Changed: , Status:
[11:48:03.807] -- Removed Source: firefox.exe

What exactly do you see when running the sample?

Aytackydln commented 1 year ago

Oh sorry, I also have OnAnyPlaybackStateChanged event listened. So, whenever I close spotify, chrome etc. I only get OnAnyPlaybackStateChanged of the application I closed with playback state "Paused". I don't receive focus event.

And mostly I see an unhandled exception in the debug logs (application resumes fine)

Aytackydln commented 1 year ago

This is my code

    static MediaMonitor()
    {
        MediaManager.OnFocusedSessionChanged += MediaManager_OnFocusedSessionChanged;
        MediaManager.OnAnyPlaybackStateChanged += MediaManager_OnAnyPlaybackStateChanged;

        MediaManager.Start();
    }

    private static void MediaManager_OnFocusedSessionChanged(MediaManager.MediaSession mediaSession)
    {
        if (mediaSession == null)
        {
            HasMedia = false;
            HasNextMedia = false;
            HasPreviousMedia = false;
            MediaPlaying = false;
            return;
        }
        HasMedia = true;
        UpdateButtons(mediaSession.ControlSession.GetPlaybackInfo());
    }

    private static void MediaManager_OnAnyPlaybackStateChanged(MediaManager.MediaSession mediaSession,
        GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo)
    {
        UpdateButtons(playbackInfo);
    }

    private static void UpdateButtons(GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo)
    {
        lock (MediaManager)
        {
            HasNextMedia = playbackInfo.Controls.IsNextEnabled;
            HasPreviousMedia = playbackInfo.Controls.IsPreviousEnabled;
            MediaPlaying = playbackInfo.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;
        }
    }
Aytackydln commented 1 year ago

Sorry, seems exception is thrown and I didn't see the window:

image

DubyaDude commented 1 year ago

You not receiving an OnFocusedSessionChanged event is caused by issue https://github.com/DubyaDude/WindowsMediaController/issues/6

You receiving that exception is caused by issue https://github.com/DubyaDude/WindowsMediaController/issues/7

DubyaDude commented 1 year ago

Fix for https://github.com/DubyaDude/WindowsMediaController/issues/6 has been deployed.

As stated in https://github.com/DubyaDude/WindowsMediaController/issues/7 there is a way to work around that, but it's on Microsoft to fix.

Closing issue, if you have any other concerns don't hesitate to ask.

Aytackydln commented 1 year ago

OnAnyPlaybackStateChanged and OnPlaybackStateChanged events are called after OnFocusedSessionChanged but I guess that's a windows thing. I will be using my old implementation, thanks anyway