Repository navigation
fix: multi-file audiobook skips part of the next chapter after a file ends - #169
Open
NicolasHartmann wants to merge 1 commit into
Open
NicolasHartmann wants to merge 1 commit into
NicolasHartmann wants to merge 1 commit into
Conversation
… 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>
Owner
|
Thanks for the PR. I'll take a gander at it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
CurrentTimeMsis stored per source file (CurrentSourceFile.CurrentTimeMs).When a file ends,
AudioPlayer_MediaEnded→OpenSourceFileswitchesCurrentSourceFileIndexright away, then awaits a dispatcher call andSaveAsync()before setting the newMediaPlayer.Source.PlaybackSession_PositionChangedqueues a dispatcher callback on every tick and callsSaveAsync()each time, so the UI thread can fall behind. Callbacks queued just before the file ended then run after the index switch. They readCurrentPosition, which is still the old file's end position, and write it toNowPlaying.CurrentTimeMs, which is now the next file's slot.MediaOpenedthen seeks the new file to that position, so the next chapter starts about one previous-file length in.OpenSourceFilealso setCurrentTimeMs = 0before 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.csonly)OpenSourceFileswitches the index first, then sets the new file'sCurrentTimeMsto the target chapter'sStartTime. 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._isSwitchingSourceflag: set before the index or source changes (inOpenSourceFileandOpenAudiobook), cleared inMediaOpenedafter the start position is applied (and inMediaFailed). While it is set,PositionChangeddoes not record positions.PositionChangedreads 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.[PlayerVM]logging (Debug.WriteLine) for source switches, the seek onMediaOpened, 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:
On the second book, the race occurred and was caught:
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
MediaPlayertiming, so I couldn't add an automated test.🤖 Generated with Claude Code