The library is available here: https://github.com/Descolada/AHK-v2-libraries/blob/main/Lib/Media.ahk
Implements the following methods and properties:
Example 1. Get info about the currently playing track, and play/pause the trackMedia 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
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()
}
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]
}
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)
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.