evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Examples

Now playing

The song your music player is playing, with its cover and a progress bar, plus keys to pause and skip without leaving the game. It reads the same media controls Windows shows when you press a volume key.

Needs: no permission.

Lua
-- @name Now playing
-- @description The current song with its cover, and keys to pause and skip.

local app = ui.text_input("App filter", "", 32)
local pause_key = ui.keybind("Play or pause", vk.f9, "hold")
local next_key = ui.keybind("Next track", vk.f10, "hold")

local function filter()
    local f = app:get()
    return f ~= "" and f or nil
end

-- a keybind in hold mode is active while held: act once when it goes active
local was_pause, was_next = false, false

events.on("paint", function()
    local p, n = pause_key:active(), next_key:active()
    if p and not was_pause then media.play_pause(filter()) end
    if n and not was_next then media.next(filter()) end
    was_pause, was_next = p, n

    local s = media.session(filter())
    if not s or s.title == "" then return end

    local sw = draw.screen_size()
    local w, h = 300, 64
    local x, y = sw - w - 20, 60
    draw.rect_filled(x, y, w, h, color(17, 17, 17, 220), 6)

    local tx = x + 8
    if s.cover then
        draw.image(s.cover, x + 8, y + 8, 48, 48)
        tx = x + 64
    end

    draw.clip_push(tx, y, x + w - 8 - tx, h)
    draw.text(tx, y + 10, s.title, color(255, 255, 255), {font = "bold"})
    draw.text(tx, y + 28, s.artist, color(170, 170, 170))
    draw.clip_pop()

    if s.duration > 0 then
        local frac = math.clamp(s.position / s.duration, 0, 1)
        draw.rect_filled(tx, y + h - 10, (x + w - 8 - tx) * frac, 3, s.playing and ui.accent() or color(120, 120, 120))
    end
end)

How it works

  • media.session(filter) gives the system's current media session, or the first one whose app name contains the filter. Leave the filter empty to follow whatever is playing. It is read from a cached copy, so calling it every frame is cheap.
  • The session has title, artist, album, playing, position and duration in seconds, and cover for draw.image once the cover art has loaded.
  • The keybinds use hold mode, and the script compares each one with its state on the last frame. That way a press skips one track, not one track per frame.
  • draw.clip_push cuts off long titles at the edge of the box.

Try this

  • Add a "Previous track" key with media.previous.
  • Show the time as 1:23 / 3:45. Round first, because %d refuses fractions: local t = math.floor(s.position), then string.format("%d:%02d", t // 60, t % 60).