WinRT/UWP Media

Post your working scripts, libraries and tools.
Descolada
Posts: 1164
Joined: 23 Dec 2021, 02:30

WinRT/UWP Media

22 Jan 2024, 14:40

This library implements WinRT/UWP Media Control (Windows.Media.Control), allowing access and control of active media sessions. It can't access any media, only UWP ones that are notified by a tray tip with the play-pause buttons and playing song title.

The library is available here: https://github.com/Descolada/AHK-v2-libraries/blob/main/Lib/Media.ahk

Implements the following methods and properties:
Media class main methods:
Media.GetCurrentSession() => Gets the current session. This is the session the system believes the user would most likely want to control.
Media.GetSessions() => Gets all of the available sessions.
Media.AddCurrentSessionChangedEvent(funcObj) => Registers a new event handler that is called when the current session changes.
Media.AddSessionsChangedEvent(funcObj) => Registers a new event handler that is called when a session is added/removed

Session class methods:
Session.Play()
Session.Pause()
Session.TogglePlayPause()
Session.Stop()
Session.Record()
Session.FastForward()
Session.Rewind()
Session.SkipNext()
Session.SkipPrevious()
Session.ChangeChannelUp()
Session.ChangeChannelDown()
Session.ChangeAutoRepeatMode(mode)
Session.ChangePlaybackRate(rate)
Session.ChangeShuffleActive(state)
Session.ChangePlaybackPosition(pos)
Session.AddTimelinePropertiesChangedEvent(funcObj)
Session.AddPlaybackInfoChangedEvent(funcObj)
Session.AddMediaPropertiesChangedEvent(funcObj)

Session properties
Session.SourceAppUserModelId => Gets the App user model Id of the source app of the session. This is usually the app executable name, for example Spotify.exe or Chrome.exe.
Session.Title
Session.Subtitle
Session.AlbumArtist
Session.Artist
Session.AlbumTitle
Session.TrackNumber
Session.Genres => Returns an array of genres
Session.AlbumTrackCount
Session.Thumbnail => Returns a IRandomAccessStreamWithContentType object of the thumbnail. This can be converted to other formats with, for example, the ImagePut library.
Session.StartTime
Session.EndTime
Session.MinSeekTime
Session.MaxSeekTime
Session.Position
Session.LastUpdatedTime
Session.PlaybackStatus
Session.PlaybackType
Session.AutoRepeatMode
Session.PlaybackRate
Session.IsShuffleActive
Session.IsPlayEnabled
Session.IsPauseEnabled
Session.IsStopEnabled
Session.IsRecordEnabled
Session.IsFastForwardEnabled
Session.IsRewindEnabled
Session.IsNextEnabled
Session.IsPreviousEnabled
Session.IsChannelUpEnabled
Session.IsChannelDownEnabled
Session.IsPlayPauseToggleEnabled
Session.IsShuffleEnabled
Session.IsRepeatEnabled
Session.IsPlaybackRateEnabled
Session.IsPlaybackPositionEnabled
Example 1. Get info about the currently playing track, and play/pause the track

Code: Select all

#Requires AutoHotkey v2
#include Media.ahk

session := Media.GetCurrentSession()

MsgBox "App with the current session: " session.SourceAppUserModelId
    . "`nPlayback state: " Media.PlaybackStatus[session.PlaybackStatus] 
    . "`nTitle: " session.Title
    . "`nArtist: " session.Artist
    . "`nPosition: " session.Position "s (duration: " session.EndTime "s)"

if (MsgBox(session.PlaybackStatus = Media.PlaybackStatus.Playing ? "Pause media?" : "Play media?",, 0x4) = "Yes") {
    if session.PlaybackStatus = Media.PlaybackStatus.Playing
        session.Pause()
    else
        session.Play()
}
Example 2. Capture events for when playback info changes

Code: Select all

#Requires AutoHotkey v2
#include Media.ahk

; Start/stop the current session media to capture an event
handle := Media.GetCurrentSession().AddPlaybackInfoChangedEvent(PlaybackInfoChangedEventHandler)
Persistent()

PlaybackInfoChangedEventHandler(session, *) {
    MsgBox "Playback info changed!"
        . "`nPlaybackStatus: " Media.PlaybackStatus[session.PlaybackStatus]
        . "`nPlaybackType: " Media.PlaybackType[session.PlaybackType]
}
Example 3. Copy the thumbnail into the Clipboard (also requires iseahounds ImagePut library)

Code: Select all

#Requires AutoHotkey v2
#include \Media.ahk
#include ImagePut.ahk ; Download here: https://github.com/iseahound/ImagePut

thumbnail := Media.GetCurrentSession().Thumbnail
ImagePutClipboard(thumbnail.ptr)
Known problems:
1) Events always return two arguments, but only one (at best) is usable. Some kind of EventArgs object is the second argument (eg PlaybackInfoChangedEventArgs), but I can't figure out what it represents or how it's useful.
2) Don't know a good way to separate two active sessions from one app, for example if Chrome has two playing tabs then they can be only separated by checking the playing media, not by tab name etc.
Last edited by Descolada on 23 Jan 2024, 00:12, edited 1 time in total.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WinRT/UWP Media

22 Jan 2024, 20:24

What application were you using to play media when creating the examples (and testing for the development of this library)? Just curious. This is pretty neat stuff!
Descolada
Posts: 1164
Joined: 23 Dec 2021, 02:30

Re: WinRT/UWP Media

23 Jan 2024, 00:08

@kczx3 I mostly tested in the Spotify desktop app, but the examples also work in Chrome and Firefox (eg when playing YouTube).
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WinRT/UWP Media

23 Jan 2024, 08:22

Cool, thanks!
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: WinRT/UWP Media

25 Jan 2024, 12:03

Could this solve situations when there are multiple media players? Such as YouTube when Spotify is open. I assume that's what is meant by GetCurrentSessions
Descolada
Posts: 1164
Joined: 23 Dec 2021, 02:30

Re: WinRT/UWP Media

25 Jan 2024, 13:06

@iseahound, yes you can differentiate between Chrome and the Spotify app:

Code: Select all

#Requires AutoHotkey v2
#include Media.ahk

for session in Media.GetSessions() {
    if session.SourceAppUserModelId = "spotify.exe" {
        MsgBox "Spotify playback status: " session.PlaybackStatus
    }
}
As far as I know it can't differentiate between multiple sessions in one app. For example if Chrome had two tabs open with YouTube and Spotify web app, then it can only access one of them - the latest one.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 26 guests