Resume unknown episode positions from progress, not 0

The web player only knows a podcast episode's position from a handoff or
a seek. When Next, auto-advance, a click or a play command makes an
episode current, it now reports `position_unknown` instead of 0, and the
server takes the resume position (and the register generation) from the
relayed progress register before it hands the state to another device.
Remote Next/Prev/play commands to a device do the same.

Next with repeat all or shuffle wraps past a trailing unplayable item
instead of stopping on it. Prev leaves an unplayable item instead of
"restarting" it, and seeking such an item moves its kept position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-19 19:21:25 +02:00
1 parent ab7202fc73
commit d1be583d1e
8 files changed
+435 -30

No files matched your search

+47 -12
View File
@@ -757,6 +757,9 @@ document.addEventListener('alpine:init', () => {
_listenedSeconds: 0,
_lastAudioTime: 0,
_localSourceTrackId: null,
// The current item is one this player cannot play and nobody told it
// that item's position (see _showUnplayableQueueItem).
_positionUnknown: false,
_remoteExecuting: false,
_remoteStateBaseTime: 0,
_remoteStateReceivedAt: 0,
@@ -888,10 +891,16 @@ document.addEventListener('alpine:init', () => {
}
if (this._isUnplayableQueueItem(track)) {
// Podcast episodes stay in the queue; this player skips them.
const playable = this._playableQueueIndex(queue.tracks, idx, idx >= previousIndex ? 1 : -1);
const step = idx >= previousIndex ? 1 : -1;
let playable = this._playableQueueIndex(queue.tracks, idx, step);
if (playable < 0 && (this.repeatMode === 'all' || this.shuffle)) {
// Wrap around like the queue itself does.
playable = this._playableQueueIndex(queue.tracks, step > 0 ? 0 : queue.tracks.length - 1, step);
}
if (playable < 0) {
// Staying on the current item keeps its position.
this._showUnplayableQueueItem(track, track === this.currentTrack ? this.currentTime : 0);
// Staying on the current item keeps its position; any
// other item's position is unknown here, never 0.
this._showUnplayableQueueItem(track, track === this.currentTrack ? this._knownPosition() : null);
return;
}
queue.currentIndex = playable;
@@ -924,6 +933,7 @@ document.addEventListener('alpine:init', () => {
return;
}
this._recordHistory('replaced');
this._positionUnknown = false;
this.currentTrack = track;
this._historyTrack = track;
this._prefetchedQueueKey = null;
@@ -994,12 +1004,17 @@ document.addEventListener('alpine:init', () => {
let idx = this._playableQueueIndex(tracks, Number(queue?.currentIndex ?? -1) + 1, 1);
if (idx < 0 && this.repeatMode === 'all') idx = this._playableQueueIndex(tracks, 0, 1);
if (idx < 0) {
this._showUnplayableQueueItem(this.currentTrack, this.currentTime);
this._showUnplayableQueueItem(this.currentTrack, this._knownPosition());
return;
}
this.playQueueIndex(idx);
},
_knownPosition() {
// The current item's position, or null when this player was never told it.
return this._positionUnknown ? null : this.currentTime;
},
toggle() {
if (!this.currentTrack) return;
if (this.isPlaying) { this.pause(); }
@@ -1020,6 +1035,11 @@ document.addEventListener('alpine:init', () => {
this._sendRemote('seek', { time: nextTime });
return;
}
if (this._isUnplayableQueueItem(this.currentTrack)) {
// Nothing is loaded in <audio>: the seek sets the item's position.
this._showUnplayableQueueItem(this.currentTrack, nextTime);
return;
}
audio.currentTime = nextTime;
this._lastAudioTime = audio.currentTime || 0;
},
@@ -1027,7 +1047,7 @@ document.addEventListener('alpine:init', () => {
seekRelative(delta) {
if (!this.currentTrack) return;
const duration = this.duration || audio.duration || 0;
const current = this._shouldSendRemote() ? this.currentTime : audio.currentTime;
const current = this._shouldSendRemote() || this._isUnplayableQueueItem(this.currentTrack) ? this.currentTime : audio.currentTime;
const max = duration > 0 ? duration : Number.MAX_SAFE_INTEGER;
this.seek(Math.max(0, Math.min(max, current + delta)));
},
@@ -1105,7 +1125,10 @@ document.addEventListener('alpine:init', () => {
this._sendRemote('prev');
return;
}
if (this.currentTime > 3) {
// An item this player cannot play is not playing here, so there
// is nothing to restart: Prev goes to the previous item.
const restartable = !this._isUnplayableQueueItem(this.currentTrack);
if (restartable && this.currentTime > 3) {
this.seek(0);
return;
}
@@ -1117,7 +1140,7 @@ document.addEventListener('alpine:init', () => {
if (this.repeatMode === 'all') {
prevIdx = queue.tracks.length - 1;
} else {
this.seek(0);
if (restartable) this.seek(0);
return;
}
}
@@ -1256,11 +1279,15 @@ document.addEventListener('alpine:init', () => {
const position = this._currentItemHasLocalAudio(track)
? (audio.currentTime || this.currentTime || 0)
: (this.currentTime || 0);
return this._remotePlaybackPayload(track, {
const payload = this._remotePlaybackPayload(track, {
position_seconds: position,
duration_seconds: this._trackDuration(),
paused: !this.isPlaying,
});
// The server fills the episode's resume position from its progress
// register instead of handing 0 to the next device.
if (this._positionUnknown && !this._currentItemHasLocalAudio(track)) payload.position_unknown = true;
return payload;
},
_mirrorRemoteTrack(track, playing, positionSeconds = null) {
@@ -1322,7 +1349,7 @@ document.addEventListener('alpine:init', () => {
this._remoteStateReceivedAt = Date.now();
if (this._isUnplayableQueueItem(track)) {
this._showUnplayableQueueItem(track, desiredTime);
this._showUnplayableQueueItem(track, state.position_unknown ? null : desiredTime);
this._updateMediaSession();
return;
}
@@ -1388,7 +1415,10 @@ document.addEventListener('alpine:init', () => {
// This device is now active, so _applyRemotePlaybackState
// does not set the time: keep the handed-off position as
// the item's position and unload the previous audio.
this._showUnplayableQueueItem(track, payload.position_seconds);
// Play commands start an item from 0, which for an
// episode means "its resume position", unknown here.
const known = command.command === 'transfer_state' && payload.position_unknown !== true;
this._showUnplayableQueueItem(track, known ? payload.position_seconds : null);
} else if (track) {
this._playLocal(track, payload);
}
@@ -1469,7 +1499,10 @@ document.addEventListener('alpine:init', () => {
return !!track && !track.unavailable && !!track.stream_url && !this._isUnplayableQueueItem(track);
},
_showUnplayableQueueItem(track, positionSeconds = 0) {
_showUnplayableQueueItem(track, positionSeconds = null) {
// `positionSeconds` null: nobody told this player where the item
// was left (it only knows positions from handoffs and seeks).
this._positionUnknown = positionSeconds == null;
// Unload the previous source so a later play() cannot resume it.
audio.pause();
audio.removeAttribute('src');
@@ -1596,7 +1629,9 @@ document.addEventListener('alpine:init', () => {
queue.currentIndex = idx;
const currentTrack = restored.tracks[idx];
if (this._isUnplayableQueueItem(currentTrack)) {
this._showUnplayableQueueItem(currentTrack, (state.position_ms || 0) / 1000);
// A saved 0 is treated as unknown: the server then
// uses the episode's progress register.
this._showUnplayableQueueItem(currentTrack, state.position_ms ? state.position_ms / 1000 : null);
} else if (currentTrack) {
this.currentTrack = currentTrack;
this._historyTrack = currentTrack;