From 116b67e51fc35ecd79bba1e1c060ca0b40a03ff0 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 12 Nov 2026 09:39:56 +0100 Subject: [PATCH] Add direct Play item presets in a regular Playlist folder --- .../media-player-classic/README.md | 19 ++++++ .../media-player-classic/companion/HELP.md | 8 +++ .../media-player-classic/src/definitions.js | 63 +++++++++++++------ .../media-player-classic/src/main.js | 11 ++++ .../media-player-classic/src/player.js | 21 ++++++- .../test/lifecycle.test.js | 2 +- .../test/playlist.test.js | 43 +++++++++++++ 7 files changed, 144 insertions(+), 23 deletions(-) diff --git a/companion-modules/media-player-classic/README.md b/companion-modules/media-player-classic/README.md index 6bbba2e..eb68c74 100644 --- a/companion-modules/media-player-classic/README.md +++ b/companion-modules/media-player-classic/README.md @@ -97,6 +97,25 @@ with the player, follows seeks, and does not predict wall-clock completion at nonstandard playback speeds. Playback state is MPC's reported state, not proof that frames or audio are physically reaching an output device. +## Playlist — direct playback + +**Presets → MPC_DEV → Playlist** contains **Play item 1–10**. Each sends MPC-HC's +normal indexed playlist selection command, which opens and plays that item. +These presets do **not** mute, stop, retry Stop, or run the experimental loading +sequence. Existing mute remains unchanged. Even the currently selected item is +sent through MPC-HC's normal open/play path. + +The reusable **Play playlist item** action accepts a 1-based item number from +1–10000. Duplicate a preset, change its action number and button text to create +additional positions. The action checks `/playlist.json` before sending selection; +missing items send no playback command. Positions follow the current playlist +order, so keep main/backup playlists aligned. Player target defaults to Both. + +The buttons stay muted blue with a play icon; they do not claim individual-item +playing feedback. Normal player feedback updates after selection. These actions +leave **Last load result** unchanged because they are not experimental loads. +Like other transport actions, they are ignored while an experimental load is running. + ## EXPERIMENTAL — Load next / previous / playlist item: not a guaranteed cue/preload > **EXPERIMENTAL: these buttons deliberately start the next or previous video, diff --git a/companion-modules/media-player-classic/companion/HELP.md b/companion-modules/media-player-classic/companion/HELP.md index a557447..b3b319f 100644 --- a/companion-modules/media-player-classic/companion/HELP.md +++ b/companion-modules/media-player-classic/companion/HELP.md @@ -30,6 +30,14 @@ while playing, full red while stopped, and amber while paused. Timers stay dark with progress bars; backup sync keeps its original status colors. Re-add copied presets to apply styling updates; linked presets update automatically. +## Playlist — direct playback + +The **Playlist** folder has **Play item 1–10** presets. These directly select and +play the numbered item using MPC-HC's normal command; there is no mute/Stop +sequence. Mute remains unchanged. Duplicate a preset and change the action's item +number and button text for other positions. Numbers follow current playlist order; +keep backup order aligned. Missing items are rejected before selection. + ## EXPERIMENTAL — Load next / Load previous Find **Load previous** and **Load next** in the separate **Experimental** preset diff --git a/companion-modules/media-player-classic/src/definitions.js b/companion-modules/media-player-classic/src/definitions.js index 014202c..c0e5680 100644 --- a/companion-modules/media-player-classic/src/definitions.js +++ b/companion-modules/media-player-classic/src/definitions.js @@ -1,7 +1,7 @@ import { commands } from './commands.js' import { colors } from './feedbacks.js' -export function actions(send, loadNext, loadPrevious, loadPlaylistItem) { +export function actions(send, loadNext, loadPrevious, loadPlaylistItem, playPlaylistItem) { const definitions = Object.fromEntries( Object.entries(commands).map(([id, name]) => [ id, @@ -36,23 +36,28 @@ export function actions(send, loadNext, loadPrevious, loadPlaylistItem) { callback: (action) => callback(action?.options?.target ?? 'both'), } } - if (loadPlaylistItem) - definitions.load_playlist_item = { - name: 'Load playlist item', - options: [ - { - type: 'number', - id: 'item', - label: 'Playlist item number (1 = first)', - default: 1, - min: 1, - max: 10000, - step: 1, - }, - ...definitions['887'].options, - ], - callback: (action) => loadPlaylistItem(action?.options?.item ?? 1, action?.options?.target ?? 'both'), - } + for (const [id, name, callback] of [ + ['load_playlist_item', 'Load playlist item', loadPlaylistItem], + ['play_playlist_item', 'Play playlist item', playPlaylistItem], + ]) { + if (callback) + definitions[id] = { + name, + options: [ + { + type: 'number', + id: 'item', + label: 'Playlist item number (1 = first)', + default: 1, + min: 1, + max: 10000, + step: 1, + }, + ...definitions['887'].options, + ], + callback: (action) => callback(action?.options?.item ?? 1, action?.options?.target ?? 'both'), + } + } return definitions } @@ -182,7 +187,22 @@ export function presets(label = 'MPC_DEV') { ] }), ) - return { ...transport, ...playlist, ...catalog } + const playPlaylist = Object.fromEntries( + Array.from({ length: 10 }, (_, index) => { + const item = index + 1 + return [ + `playlist_play_item_${item}`, + { + type: 'simple', + name: `Play item ${item}`, + style: { text: `▶\nPLAY\n${item}`, size: 12, color: colors.white, bgcolor: 0x243b50, show_topbar: true }, + steps: [{ down: [{ actionId: 'play_playlist_item', options: { item, target: 'both' } }], up: [] }], + feedbacks: [offline], + }, + ] + }), + ) + return { ...transport, ...playlist, ...playPlaylist, ...catalog } } export function presetSections(buttons) { @@ -204,6 +224,11 @@ export function presetSections(buttons) { ...Object.keys(buttons).filter((id) => id.startsWith('transport_load_item_')), ], }, + { + id: 'playlist', + name: 'Playlist', + definitions: Object.keys(buttons).filter((id) => id.startsWith('playlist_play_item_')), + }, { id: 'commands', name: 'All commands', definitions: Object.keys(commands) }, ] } diff --git a/companion-modules/media-player-classic/src/main.js b/companion-modules/media-player-classic/src/main.js index 98272a0..9e85906 100644 --- a/companion-modules/media-player-classic/src/main.js +++ b/companion-modules/media-player-classic/src/main.js @@ -16,6 +16,7 @@ export default class MpcInstance extends InstanceBase { (target) => this.loadItem('next', target), (target) => this.loadItem('previous', target), (item, target) => this.loadItem(`item_${item}`, target), + (item, target) => this.playPlaylistItem(item, target), ), ) this.setFeedbackDefinitions( @@ -172,6 +173,16 @@ export default class MpcInstance extends InstanceBase { } } + async playPlaylistItem(item, target = 'both') { + if (this.loadingItem) { + this.log('warn', 'Command ignored while experimental loading is running') + return + } + if (!['both', 'main', 'backup'].includes(target)) return + const players = target === 'main' ? [this.main] : target === 'backup' ? [this.backup] : [this.main, this.backup] + await Promise.allSettled(players.filter(Boolean).map((player) => player.playPlaylistItem(item))) + } + async send(id, target = 'both') { if (this.loadingItem) { this.log('warn', 'Command ignored while experimental loading is running') diff --git a/companion-modules/media-player-classic/src/player.js b/companion-modules/media-player-classic/src/player.js index d95fab8..3f3977e 100644 --- a/companion-modules/media-player-classic/src/player.js +++ b/companion-modules/media-player-classic/src/player.js @@ -107,17 +107,32 @@ export class Player { return this.loading } + playPlaylistItem(item) { + return this.runCommand(async () => { + const number = Number(item) + if (!Number.isInteger(number) || number < 1 || number > 10000) throw new Error('Invalid playlist item number') + const playlist = await this.client.playlist() + if (number > playlist.items.length) + throw new Error(`Playlist item ${number} does not exist (${playlist.items.length} items)`) + await this.client.selectPlaylistItem(number - 1) + }) + } + send(id) { + return this.runCommand(() => this.client.command(id)) + } + + runCommand(command) { if (this.loading) return Promise.resolve(false) - const pending = this.sendCommand(id) + const pending = this.sendCommand(command) this.commands.add(pending) return pending.finally(() => this.commands.delete(pending)) } - async sendCommand(id) { + async sendCommand(command) { if (!this.active) return try { - await this.client.command(id) + await command() await this.pending if (this.active) await this.refresh() } catch (error) { diff --git a/companion-modules/media-player-classic/test/lifecycle.test.js b/companion-modules/media-player-classic/test/lifecycle.test.js index 49f772a..9397cbd 100644 --- a/companion-modules/media-player-classic/test/lifecycle.test.js +++ b/companion-modules/media-player-classic/test/lifecycle.test.js @@ -47,7 +47,7 @@ test('initialization registers API 2 presets; successful actions recover failed await module.init({ host: '127.0.0.1', port: server.address().port }) await module.send('887') assert.equal(state.statuses.at(-1), InstanceStatus.Ok) - assert.equal(Object.keys(state.actions).length, 141) + assert.equal(Object.keys(state.actions).length, 142) assert.deepEqual(state.sections.flatMap((section) => section.definitions).sort(), Object.keys(state.presets).sort()) assert.equal(state.variables.remaining, '00:48') assert.equal(state.feedbacks.playing.callback(), true) diff --git a/companion-modules/media-player-classic/test/playlist.test.js b/companion-modules/media-player-classic/test/playlist.test.js index 5cf7288..100925d 100644 --- a/companion-modules/media-player-classic/test/playlist.test.js +++ b/companion-modules/media-player-classic/test/playlist.test.js @@ -150,3 +150,46 @@ test('modular action, first ten presets and load-result feedback agree on item n assert.equal(fb.load_playlist_item_status.callback({ options: { item: 1 } }).text, '↓\nLOAD\n1') assert.equal(fb.load_result.callback().text, 'LAST\nLOADED\n#12') }) + +test('direct playlist play sends only selection, leaves mute alone, and does not record an experimental load', async (t) => { + for (const muted of [true, false]) { + const { player, data, commands } = await mock(t) + data.muted = muted + await player.playPlaylistItem(3) + assert.deepEqual(commands, ['/command.html?wm_command=-3&index=2']) + assert.equal(data.state, 2) + assert.equal(data.muted, muted) + assert.equal(player.loadResult.status, 'idle') + assert.equal(player.state.file, 'Clip 3.mp4') + } +}) + +test('direct playlist play rejects missing items without a playback command', async (t) => { + const { player, commands } = await mock(t) + await player.playPlaylistItem(13) + assert.deepEqual(commands, []) + assert.match(player.error, /does not exist/) +}) + +test('direct playlist action and presets expose selection independently of experimental loads', async () => { + let sent + const definitions = actions( + () => {}, + null, + null, + null, + (item, target) => { + sent = { item, target } + }, + ) + await definitions.play_playlist_item.callback({ options: { item: 7, target: 'main' } }) + assert.deepEqual(sent, { item: 7, target: 'main' }) + for (let item = 1; item <= 10; item++) { + const button = presets()[`playlist_play_item_${item}`] + assert.deepEqual(button.steps[0].down, [{ actionId: 'play_playlist_item', options: { item, target: 'both' } }]) + assert.deepEqual( + button.feedbacks.map((f) => f.feedbackId), + ['offline'], + ) + } +})