Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnFocusedSessionChanged playbackState is Paused when current focused is closed #10

Closed
Aytackydln opened this issue Nov 25, 2022 · 7 comments
Labels
duplicate This issue or pull request already exists

Comments

@Aytackydln
Copy link

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"

@Aytackydln Aytackydln changed the title OnFocusedSessionChanged playbackState is Pause when current focused is closed OnFocusedSessionChanged playbackState is Paused when current focused is closed Nov 25, 2022
@DubyaDude
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

image

@DubyaDude
Copy link
Owner

DubyaDude commented Nov 25, 2022

You not receiving an OnFocusedSessionChanged event is caused by issue #6

You receiving that exception is caused by issue #7

@DubyaDude DubyaDude added the duplicate This issue or pull request already exists label Nov 26, 2022
@DubyaDude
Copy link
Owner

Fix for #6 has been deployed.

As stated in #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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants