Keep the episode position and skip unplayable items safely in the player

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-19 18:35:49 +02:00
1 parent c14ab8dfea
commit 4d0b352258
3 files changed
+152 -11

No files matched your search

+48 -11
View File
@@ -774,6 +774,9 @@ document.addEventListener('alpine:init', () => {
});
audio.addEventListener('timeupdate', () => {
// Unloading the audio for an unplayable item fires a late
// timeupdate at 0; keep that item's handed-off position.
if (!this._currentItemHasLocalAudio()) return;
this.currentTime = audio.currentTime;
this.duration = audio.duration || 0;
this.progress = this.duration > 0 ? (this.currentTime / this.duration) * 100 : 0;
@@ -887,7 +890,8 @@ document.addEventListener('alpine:init', () => {
// Podcast episodes stay in the queue; this player skips them.
const playable = this._playableQueueIndex(queue.tracks, idx, idx >= previousIndex ? 1 : -1);
if (playable < 0) {
this._showUnplayableQueueItem(track);
// Staying on the current item keeps its position.
this._showUnplayableQueueItem(track, track === this.currentTrack ? this.currentTime : 0);
return;
}
queue.currentIndex = playable;
@@ -916,7 +920,7 @@ document.addEventListener('alpine:init', () => {
_playLocal(track, options = {}) {
if (this._isUnplayableQueueItem(track)) {
this._recordHistory('replaced');
this._showUnplayableQueueItem(track);
this._showUnplayableQueueItem(track, options.position_seconds);
return;
}
this._recordHistory('replaced');
@@ -977,12 +981,25 @@ document.addEventListener('alpine:init', () => {
return;
}
if (this._isUnplayableQueueItem(this.currentTrack)) {
this.next();
// Not next(): with repeat "one" it would resume this item again.
this._skipUnplayableCurrentItem();
return;
}
audio.play().catch(() => {});
},
_skipUnplayableCurrentItem() {
const queue = Alpine.store('queue');
const tracks = queue?.tracks || [];
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);
return;
}
this.playQueueIndex(idx);
},
toggle() {
if (!this.currentTrack) return;
if (this.isPlaying) { this.pause(); }
@@ -1060,7 +1077,8 @@ document.addEventListener('alpine:init', () => {
this._recordHistory('skipped');
let nextIdx;
if (this.repeatMode === 'one') {
// Repeat "one" cannot repeat an item this player cannot play.
if (this.repeatMode === 'one' && !this._isUnplayableQueueItem(this.currentTrack)) {
this.seek(0);
this._historyRecorded = false;
this._resetPlaybackTracking();
@@ -1234,8 +1252,12 @@ document.addEventListener('alpine:init', () => {
const queue = Alpine.store('queue');
const track = this.currentTrack || queue?.tracks?.[queue.currentIndex] || null;
if (!track && (!queue || queue.tracks.length === 0)) return null;
// <audio> holds an older track while an unplayable item is current.
const position = this._currentItemHasLocalAudio(track)
? (audio.currentTime || this.currentTime || 0)
: (this.currentTime || 0);
return this._remotePlaybackPayload(track, {
position_seconds: audio.currentTime || this.currentTime || 0,
position_seconds: position,
duration_seconds: this._trackDuration(),
paused: !this.isPlaying,
});
@@ -1300,7 +1322,7 @@ document.addEventListener('alpine:init', () => {
this._remoteStateReceivedAt = Date.now();
if (this._isUnplayableQueueItem(track)) {
this._showUnplayableQueueItem(track);
this._showUnplayableQueueItem(track, desiredTime);
this._updateMediaSession();
return;
}
@@ -1363,7 +1385,10 @@ document.addEventListener('alpine:init', () => {
const track = payload.track || queue.tracks[queue.currentIndex];
if (track?.unavailable || (track && !track.stream_url)) {
this._applyRemotePlaybackState({ ...payload, track, paused: true });
this._pauseLocal();
// 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);
} else if (track) {
this._playLocal(track, payload);
}
@@ -1438,7 +1463,13 @@ document.addEventListener('alpine:init', () => {
return -1;
},
_showUnplayableQueueItem(track) {
_currentItemHasLocalAudio(track = this.currentTrack) {
// Whether <audio> can hold this item: podcast episodes, private
// items and unresolved federation items are never loaded.
return !!track && !track.unavailable && !!track.stream_url && !this._isUnplayableQueueItem(track);
},
_showUnplayableQueueItem(track, positionSeconds = 0) {
// Unload the previous source so a later play() cannot resume it.
audio.pause();
audio.removeAttribute('src');
@@ -1447,9 +1478,15 @@ document.addEventListener('alpine:init', () => {
this.currentTrack = track;
this._historyTrack = null;
this._localSourceTrackId = track?.id ?? null;
this.currentTime = 0;
this.progress = 0;
this.duration = Number(track?.duration_seconds || 0);
// Keep the item's position (handoff, restore) so it is reported back unchanged.
let position = Math.max(0, Number(positionSeconds || 0));
if (!Number.isFinite(position)) position = 0;
if (this.duration > 0) position = Math.min(position, this.duration);
this.currentTime = position;
this._remoteStateBaseTime = position;
this._remoteStateReceivedAt = Date.now();
this.progress = this.duration > 0 ? (position / this.duration) * 100 : 0;
Alpine.store('queue')?.syncCurrentIndexToTrack(track);
},
@@ -1559,7 +1596,7 @@ document.addEventListener('alpine:init', () => {
queue.currentIndex = idx;
const currentTrack = restored.tracks[idx];
if (this._isUnplayableQueueItem(currentTrack)) {
this._showUnplayableQueueItem(currentTrack);
this._showUnplayableQueueItem(currentTrack, (state.position_ms || 0) / 1000);
} else if (currentTrack) {
this.currentTrack = currentTrack;
this._historyTrack = currentTrack;