Play queue and playback controls
The play queue and playback-control system turns selected media into an ordered playback session. It keeps queue order, the active item, repeat and shuffle state, and player state in sync so that the player, system media controls, and in-app controls show the same result. See Media playback, VLC playback engine, and Video rendering for the wider playback model.
Queue session
Queue creation
The queue service creates a playback session from files, folders, playlists, media sources, or URLs. It expands collection sources into media items and records the source of the session so the rest of the app can treat a single file, a folder, and a playlist as related but distinct playback starts.
Single-file playback can keep a query for nearby files. The queue does not need to load those files at first. It adds them when the user moves past the only item, which keeps simple file playback fast while still supporting folder-style next and previous actions.
The queue has a size limit and ignores unsupported storage items. An empty parse produces no session, so the player does not start with an invalid queue.
References: Screenbox.Core/…/IQueueService.cs• L14-78 Screenbox.Core/…/QueueService.cs• L42-105 Screenbox.Core/…/QueueService.cs• L108-160 Screenbox.Core/…/QueueService.cs• L226-244
Queue ownership
Screenbox.Core/…/PlayQueueCoordinator.cs is the state owner for the app-wide queue. It keeps a queue session for navigation and writes the observable PlayQueueContext for data binding. View models ask the coordinator to change the queue instead of changing the shared state themselves.
This split gives the system one path for queue changes. Playing an item, replacing a queue, adding or removing items, clearing the queue, and resetting a failed item all update the active item, its index, the player item, navigation availability, and playback-state messages together.
The queue context exposes the ordered items, current item, current index, session identity, origin, playing state, shuffle mode, and repeat mode. The session model also keeps the current order and the information needed to restore the unshuffled order.
References: Screenbox.Core/…/IPlayQueueCoordinator.cs• L8-75 Screenbox.Core/…/PlayQueueCoordinator.cs• L28-70 Screenbox.Core/…/PlayQueueCoordinator.cs• L343-450 Screenbox.Core/…/PlayQueueContext.cs• L10-60 Screenbox.Core/…/QueueSession.cs• L8-36
Navigation and buffering
Playback navigation
The playback-control service answers whether next and previous actions are valid and returns the item to play. Normal navigation moves within the session. List repeat wraps at either end. Track repeat is handled by restarting the current item when playback ends.
Previous has a user-facing restart rule: while a track is playing, a request after five seconds restarts that track instead of moving back. At the start of a queue, the same restart behavior applies when no repeat mode can provide a previous item.
For a single file with a neighboring-file query, next and previous resolve the adjacent file and create a new session around it. This makes a file opened on its own behave like the first item in its folder without forcing the whole folder into the initial queue.
References: Screenbox.Core/…/IPlaybackControlService.cs• L7-33 Screenbox.Core/…/PlaybackControlService.cs• L22-79 Screenbox.Core/…/PlaybackControlService.cs• L82-171 Screenbox.Core/…/PlayQueueCoordinator.cs• L346-389
Playback item buffer
The play queue buffer is a sliding window of native playback items around the current queue position. It prepares nearby items before they become active and disposes items that leave the window. The window also includes the opposite end of the queue when list repeat needs wrap-around access.
The coordinator updates this buffer after the current item changes. The buffer manager reuses an existing playback item when possible and creates one through the player service when needed. If creation fails, it reports a media-load error instead of leaving an untracked native item.
This buffer is separate from the media player's network or decoder buffering. The player exposes its own buffering progress and buffering events, while the queue buffer controls which queue entries have ready playback objects.
The following flow shows how a queue position becomes a set of prepared playback items.
flowchart LR
A[Current queue position] --> B[Queue buffer indices]
B --> C[Nearby media items]
C --> D[Create or reuse playback items]
D --> E[Player playback item]
C --> F[Dispose items outside the window]
References: Screenbox.Core/…/QueueService.cs• L322-438 Screenbox.Core/…/IPlayQueueBufferManager.cs• L10-46 Screenbox.Core/…/PlayQueueBufferManager.cs• L21-126 Screenbox.Core/…/IMediaPlayer.cs• L18-27
Shuffle and repeat state
Shuffle changes the active order while keeping a backup of the original order. The current item can remain first after a shuffle, so enabling shuffle does not unexpectedly change what the user is watching. Turning shuffle off restores the original order with removed items excluded.
Repeat mode belongs to the queue context and is also sent to Windows system media controls and playback settings. A change to repeat mode therefore affects both navigation decisions and the controls outside the main player window.
References: Screenbox.Core/…/QueueService.cs• L324-409 Screenbox.Core/…/PlayQueueCoordinator.cs• L305-339 Screenbox.Core/…/PlayQueueContext.cs• L42-60
Player services
Player service
The player service creates and disposes the media player and turns an app media source into a playback item that the player can use. It is the boundary between queue-owned media view models and the playback engine described in VLC playback engine.
The service keeps file access valid while a playback item lives. It releases that access when the item is disposed, which lets the queue buffer evict items without losing access management for items that remain active.
References: Screenbox.Core/…/IPlayerService.cs• L6-13 Screenbox.Core/…/PlayerService.cs• L58-80 Screenbox.Core/…/PlayerService.cs• L82-147 Screenbox.Core/…/PlayerService.cs• L150-185
Playback control service
The playback-control service contains the rules for moving through a queue. It does not own the queue or the player. The coordinator supplies the current session and repeat mode, then applies the service's navigation result to the observable queue and active player.
This separation keeps user commands, end-of-media events, and system media-control requests on the same navigation path. The source of the request changes, but the result still follows one set of next, previous, repeat, and neighboring-file rules.
References: Screenbox.Core/…/IPlaybackControlService.cs• L7-33 Screenbox.Core/…/PlaybackControlService.cs• L9-20 Screenbox.Core/…/PlayQueueCoordinator.cs• L896-906 Screenbox.Core/…/PlayQueueCoordinator.cs• L977-1001 Screenbox.Core/…/PlayQueueCoordinator.cs• L1004-1040
Playback-session view models
Playback session settings
PlaybackSessionViewModel represents settings that apply to the active playback session rather than to queue order. It exposes playback rate, audio timing offset, and subtitle timing offset for controls and menus.
When the active media player changes, the view model applies the current settings to the new player. When a setting changes, it sends the value to the player. Changing the current queue item resets the timing offsets so adjustments from one item do not silently carry into the next item.
References: Screenbox.Core/…/PlaybackSessionViewModel.cs• L13-43 Screenbox.Core/…/PlaybackSessionViewModel.cs• L46-66 Screenbox.Core/…/PlaybackSessionViewModel.cs• L80-122
Player control state
PlayerControlsViewModel is the state adapter for the main playback controls. It observes the active player and the shared queue context, then exposes simple control state such as IsPlaying, HasActiveItem, HasVideo, fullscreen state, compact-layout state, and whether next or previous commands can run.
The active player's playback-state event drives IsPlaying. The queue's current item drives whether an active item exists and resets session timing settings. Next and previous commands call the queue coordinator, so the visible buttons use the same rules as automatic track changes and system media controls.
The controls also expose playback rate menus, subtitle selection, aspect ratio, snapshot, fullscreen, compact layout, shuffle, and repeat. These actions change player settings or queue state through the view model and do not create a second playback state model.
References: Screenbox.Core/…/PlayerControlsViewModel.cs• L23-74 Screenbox.Core/…/PlayerControlsViewModel.cs• L125-171 Screenbox.Core/…/PlayerControlsViewModel.cs• L272-332 Screenbox.Core/…/PlayerControlsViewModel.cs• L389-414
User control surfaces
The main player control surface binds its buttons to the player-control view model and its queue toggles to PlayQueueContext. This exposes play or pause, previous, next, seek position, shuffle, repeat, volume, track selection, and display modes without duplicating queue state in the UI.
The play queue surface binds its list to the same queue items. It lets users play an item, insert it next, add files or URLs, reorder items, remove items, and clear the queue. These actions go through the queue coordinator, so the player and all queue views receive the same current-item and navigation updates.
The state relationship is:
flowchart LR
P[Media player events] --> C[PlayQueueContext]
Q[Queue coordinator] --> C
C --> PC[PlayerControlsViewModel]
C --> PQ[PlayQueuePanelViewModel]
PC --> UI[Player controls]
PQ --> QU[Play queue UI]
PC --> Q
PQ --> Q
References: Screenbox/…/PlayerControls.xaml• L334-423 Screenbox/…/PlayerControls.xaml• L520-580 Screenbox/…/PlayQueueControl.xaml• L218-260 Screenbox.Core/…/PlayQueuePanelViewModel.cs• L17-32 Screenbox.Core/…/PlayQueuePanelViewModel.cs• L83-215
Change checklist
When these files or sections are changed, remember to consider these:
- When queue mutation rules change, check the queue context, navigation availability, buffer window, and system media-control state together.
- When the current-item contract changes, check player-item assignment, timing-offset reset, playback-state broadcasts, and recent-item updates.
- When repeat or shuffle behavior changes, check both in-app bindings and Windows system media controls.
- When playback-item lifetime changes, check buffer eviction and file-access token release.
- When a new playback entry source is added, check queue parsing, neighboring-file navigation, and the player service's source mapping.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A buffered playback item cannot be created | The item cannot start and the user receives a media-load error | PlayQueueBufferManager reports the failure and returns no playback item |
| The active item is removed | Playback can keep pointing at an item no longer in the queue | The coordinator clears the active item and evicts its buffered playback item before removal |
| A single file has no neighboring-file query | Next and previous cannot move into adjacent files | Navigation checks the query before using neighboring-file behavior |
| Media ends at the last item with repeat off | Playback stops instead of moving past the queue | End handling returns no navigation result in this state |
| A native playback event arrives off the UI thread | Queue and player state can be changed from the wrong thread | The coordinator dispatches state changes and navigation work to the UI thread |
Key files
| File | Why to read it |
|---|---|
| Screenbox.Core/…/PlayQueueCoordinator.cs | Main owner of queue changes, player events, navigation, buffering updates, and state broadcasts |
| Screenbox.Core/…/PlayQueueContext.cs | Shared observable queue state used by the controls |
| Screenbox.Core/…/QueueService.cs | Queue creation, neighboring-file expansion, shuffle, and buffer-index rules |
| Screenbox.Core/…/PlaybackControlService.cs | Next, previous, repeat, and end-of-media rules |
| Screenbox.Core/…/PlayQueueBufferManager.cs | Sliding playback-item buffer and native item lifetime |
| Screenbox.Core/…/PlayerService.cs | Player creation and media-source to playback-item boundary |
| Screenbox.Core/…/PlayerControlsViewModel.cs | Playback state and commands exposed to the main controls |
| Screenbox.Core/…/PlaybackSessionViewModel.cs | Playback rate and timing settings for the active session |
| Screenbox.Core/…/PlayQueuePanelViewModel.cs | Queue actions exposed by the queue panel |
Screenbox/Controls/PlayerControls.xaml | Main control bindings for playback and queue state |
Screenbox/Controls/PlayQueueControl.xaml | Queue list and queue-management surface |