Skip to content

fix: multi-file audiobook skips part of the next chapter after a file ends - #169

Open
NicolasHartmann wants to merge 1 commit into
rstewa:mainfrom
NicolasHartmann:fix/multi-file-chapter-skip
Open

NicolasHartmann wants to merge 1 commit into
rstewa:mainfrom
NicolasHartmann:fix/multi-file-chapter-skip

Conversation

@NicolasHartmann

Copy link
Copy Markdown

Problem

With audiobooks made of multiple files (reported with mp3), playback sometimes skips a large part of the next chapter (half or ¾ of it) when one file ends and the next begins. It looks random: sometimes every other chapter, sometimes a few chapters play fine first. Mini player mode makes no difference.

Root cause

CurrentTimeMs is stored per source file (CurrentSourceFile.CurrentTimeMs).

When a file ends, AudioPlayer_MediaEnded → OpenSourceFile switches CurrentSourceFileIndex right away, then awaits a dispatcher call and SaveAsync() before setting the new MediaPlayer.Source.

PlaybackSession_PositionChanged queues a dispatcher callback on every tick and calls SaveAsync() each time, so the UI thread can fall behind. Callbacks queued just before the file ended then run after the index switch. They read CurrentPosition, which is still the old file's end position, and write it to NowPlaying.CurrentTimeMs, which is now the next file's slot. MediaOpened then seeks the new file to that position, so the next chapter starts about one previous-file length in.

OpenSourceFile also set CurrentTimeMs = 0 before switching the index, so it zeroed the old file and left the new file's stored value in place.

This is the same race that 5120508 worked around for the chapter index. That commit changed how the next chapter is chosen but not the race itself.

Fix (PlayerViewModel.cs only)

  • OpenSourceFile switches the index first, then sets the new file's CurrentTimeMs to the target chapter's StartTime. That is 0 for the automatic "file ended" case. Jumping to a chapter in another file through the chapter combo box or the prev/next chapter buttons now also lands on that chapter's start instead of a stale stored position.
  • New _isSwitchingSource flag: set before the index or source changes (in OpenSourceFile and OpenAudiobook), cleared in MediaOpened after the start position is applied (and in MediaFailed). While it is set, PositionChanged does not record positions.
  • PositionChanged reads the position and source index on the event thread, not later inside the dispatcher callback. Queued callbacks are dropped if the audiobook or source file has changed since, or a switch is in progress.
  • Debug-only [PlayerVM] logging (Debug.WriteLine) for source switches, the seek on MediaOpened, and discarded stale updates. It is compiled out of Release builds.

Testing

Tested a Debug build on two multi-file mp3 audiobooks, watching the output in Sysinternals DebugView:

[PlayerVM] OpenSourceFile: 6 -> 7, chapter 7
[PlayerVM] MediaOpened: source 7, seeking to 0 ms

On the second book, the race occurred and was caught:

[PlayerVM] Discarded stale position update: 740597 ms from source 1, current source is now 2 (switching: True)

Without the fix, those 740597 ms (about 12:20) would have been written into file 2's position, so the next chapter would have started 12:20 in. With the fix, the next file starts at 0 ms, and there were no more skips while listening.

There is no test project in the repo, and the race depends on WinUI MediaPlayer timing, so I couldn't add an automated test.

🤖 Generated with Claude Code

… ends

CurrentTimeMs is stored per source file. When a file ended,
PositionChanged dispatcher callbacks that were still queued on a busy UI
thread ran after OpenSourceFile had already switched CurrentSourceFileIndex.
They read the old file's end position and wrote it into the *next* file's
CurrentTimeMs, so MediaOpened seeked the new file forward by roughly the
length of the previous chapter. Timing-dependent, so it appeared random.

- OpenSourceFile switches the index first, then sets the new file's
  position to the target chapter's start (it used to zero the old file and
  keep the new file's stale value).
- A _isSwitchingSource flag (set before the switch, cleared after
  MediaOpened applied the start position) stops PositionChanged from
  recording positions during the switch.
- PositionChanged captures position and source index on the event thread
  and discards queued updates whose source or audiobook has changed.
- Debug-only [PlayerVM] logging of switches and discarded updates.

Verified on two multi-file mp3 audiobooks: a stale update (740597 ms
from source 1 while switching to source 2) was discarded and the next
file started at 0 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rstewa

rstewa commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks for the PR. I'll take a gander at it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants