Search code examples
c++uwpvisual-studio-2019c++-winrt

Retrive Informations about currently running sessions using Windows.Media.Control with C++/WinRT


I would like to know, how to retrive informations(e.g application name) about all the sessions that are currently running.

GlobalSystemMediaTransportControlsSessionManager SessionManager();
IVectorView<GlobalSystemMediaTransportControlsSession> Sessions;
Sessions = SessionManager.GetSessions();
// for sessions - session.SourceAppUserModelId

I want to learn WinRT with C++, so I ve been trying to do something with Windows Media Control, but looking at the documentation: https://learn.microsoft.com/en-us/uwp/api/windows.media.control?view=winrt-22621, https://learn.microsoft.com/en-us/uwp/api/windows.media.control.globalsystemmediatransportcontrolssessionmanager?view=winrt-22621 I have no idea what I should do. I would appreciate any links to tutorials or explanations on how to do it or learn it.


Solution

  • Using, trial and error method, I have finally achived what I was looking for, in a way. Hopefully, it will be usefull to someone other, than me.

    I didn't know, you have to pass NULL as parameter to GlobalSystemMediaTransportControlsSessionManager class, for some reason.
    Also, had some problem with converting hstring to string, but I found a function winrt::to_string().

        GlobalSystemMediaTransportControlsSessionManager SessionManager(NULL);
        // get SessionManager to provide access to info of playback
        if (SessionManager == NULL) {
            // gets SessionManager instance
            IAsyncOperation session_async = SessionManager.RequestAsync();
            // waits 5 seconds before failure
            if (session_async.wait_for(std::chrono::seconds{ 5 }) == AsyncStatus::Completed) {
                SessionManager = session_async.GetResults();
            }
            else {
                std::cout << "Couldnt request instance of Session Manager" << std::endl;
            }
            std::cout << "Done" << std::endl;
        }
        
        IVectorView<GlobalSystemMediaTransportControlsSession> sessions = SessionManager.GetSessions();
        
        int i = 0;
        for (auto const &session : sessions) {
            std::cout << i << ' ';
            // get Application name
            winrt::hstring session_name = session.SourceAppUserModelId();
            std::cout << " name- " << winrt::to_string(session_name);
    
            // pause session
            IAsyncOperation<bool> paused = session.TryPauseAsync();
            i++;
        }