Skip to content

feat: step one narration cue with next/previous in media-overlay books - #333

Open
ddfreiling wants to merge 7 commits into
Notalib:mainfrom
ddfreiling:feat/media-overlay-cue-step
Open

ddfreiling wants to merge 7 commits into
Notalib:mainfrom
ddfreiling:feat/media-overlay-cue-step

Conversation

@ddfreiling

@ddfreiling ddfreiling commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

In media-overlay books (sync narration, or guided navigation with audio), next() and previous() seeked by the time interval, so a listener could not step through the narration sentence by sentence. Now each call steps one narration cue on iOS, Android and web. TTS and plain audiobooks are unchanged, and the public API is unchanged: audioSeekBy remains the way to skip by time.

Change

  • Cue stepping: the cue list is flat across the publication, so stepping crosses chapter boundaries. next() on the last cue does nothing.
  • previous() follows music-player rules: more than 2 s into a cue restarts that cue; otherwise it goes to the previous cue. On the first cue it restarts that cue.
  • Play state is kept: a step while paused stays paused, and a step while playing keeps playing.
  • Narration sync: a step re-enables narration sync with exactly one true emission, and the text never flashes back to the old cue.
  • Remote controls: lock-screen and remote next/previous still seek by the interval.
  • Rapid steps are serialised on every platform, so two quick next() calls land two cues on: a promise chain on web, a Mutex on Android, and on iOS a Task chain on the MainActor that every audio go(to:) goes through, including goToLocator and goToProgression.
  • Steps start from the live position:
    • Android reads navigator.playback.value instead of the derived locator.
    • iOS reads the player's playbackInfo.
    • Web reads currentTime while playing.
  • Timebased state follows the step: Android writes the cue's locator to state, as goToLocator does. The navigator's throttled locator flow drops a step back to a position it already reported.

Review notes

  • iOS queues every goBounded call because overlapping calls clear _isNavigating early, which re-exposes a known deadlock. Code already inside the queue uses goBoundedUnqueued.
  • On web, the sync-narration lookup is evaluated against _audioNavPublication on purpose. The cached _hasSyncNarration describes _publication, which can differ.
  • Platform cue logic is in cueStepTarget/skipToCue on iOS and Android, and in findCueStepTarget/_stepCue on web.

Verification

  • iOS CueStepTargetTests pass.
  • Android FlutterMediaOverlayTest: 40/40 pass.
  • Jest: 453/453 pass across 28 suites. The new stale-time test fails without the live-clock fix.
  • bin/typecheck, bin/format and bin/analyze are clean.
  • The cue-step integration tests in timebased_playback_test.dart pass in CI on iOS, Android API 24 and Android API 36. They cover a rapid double next(), the single sync emission, and stepping while playing.

Not covered

  • Web was not run in a real browser. It is covered by Jest only.
  • Comics (DiViNa guided navigation) were not checked at runtime.
  • iOS step ordering relies on MainActor FIFO scheduling of the queued tasks, which holds in practice but is not a documented guarantee.
  • Follow-up: keyboard paging. In guided publications the arrow keys should step cues, and PageUp/PageDown should turn pages.
  • Follow-up: in plain audiobooks on web, next/previous go to the next or previous audio file, while native seeks by the interval.

🤖 Generated with Claude Code

ddfreiling and others added 3 commits November 16, 2026 11:18
next() and previous() now move one cue (sentence or panel) in
publications with sync narration or guided navigation, on iOS,
Android and web. The cue list is flat across the publication, so a
step crosses resource boundaries.

- previous() restarts the current cue when more than 2 s into it,
  otherwise goes to the previous cue; next() on the last cue is a
  no-op.
- Play state is kept; the view follows the target cue while paused.
- A step re-enables narration sync from manual mode without flashing
  the old cue first.
- A seek landing up to 0.1 s before a cue start counts as that cue.

TTS (one utterance) and plain audiobooks are unchanged. Lock-screen
and remote next/previous keep the seek interval, and audioSeekBy
remains the way to skip by time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review fixes for media-overlay next/previous:

- iOS: chain cue steps. Overlapping steps ran goBounded
  concurrently, so the first to finish cleared _isNavigating while
  the second go(to:) was still running, and both read the same
  position. A locator without a time fragment now steps from 0, as
  on Android, instead of doing nothing.
- Android: guard cue steps with a mutex, since method calls run in
  parallel coroutines, and read the position from playback state,
  which is current as soon as the previous seek settles.
- Web: while playing, step from the media clock; the polled locator
  can still name the previous cue.

Tests: Jest for two quick steps and the lagging locator; integration
tests for a rapid double next(), stepping while playing, and a settle
delay before the single sync-emission assert.

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

* origin/main:
  ci(android): retry the suite when adb install hangs on API 24
@ddfreiling ddfreiling added the low-priority This issue or PR is of low priority, do not take it first. label Nov 17, 2026
@m-abs

m-abs commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Findings from kb-review:

Should fix

  1. iOS: the new serialisation only protects next/previous from each other (FlutterAudioNavigator.swift:212-248; callers at FlutterMediaOverlayNavigator.swift:133,172, FlutterReadiumPlugin.swift:455,482) confirmed; correctness + lean → fix
    Problem: The chain in enqueueCueStep was added because two overlapping goBounded calls clear _isNavigating as soon as the first go(to:) returns, while the second is still running. But goToLocator and goToProgression (channel calls at FlutterReadiumPlugin.swift:455 and :482) go through seek(toLocator:) / seek(toProgression:), which call the same goBounded — and are not on the chain. Before this PR next() used seekRelative, so it never entered goBounded; the diff makes that collision reachable from a new call site.
    Failure case: the app seeks with the progress slider (goToProgression) while a cue step is still in flight → two concurrent go(to:) → the first to finish drops _isNavigating → the mid-navigation delegate callbacks the guard exists to suppress run again, which is the hang the comment describes.
    Fix: move the serialisation inside goBounded itself (chain every go(to:) on the navigator). That fixes the shared defect and deletes cueStepTask + enqueueCueStep (~12 lines) instead of adding them.
  2. iOS still steps from the last reported position, not the live player clock (FlutterMediaOverlayNavigator.swift:149-151) confirmed by reading; correctness → fix
    Problem: audioLocator is only written from locationDidChange, which upstream fires once per playbackRefreshInterval tick (swift-toolkit 3.11.1 AudioNavigator.swift:271-281, 336-342). So the step starts from a position up to one poll interval stale. The same commit fixed exactly this on Android (playback.value) and web (currentTime). The repo already knows this value is stale: FlutterAudioNavigator.swift:615 re-overrides it with playback.time.
    Failure case: playing with a 1 s poll; you are 0.9 s into cue N but the last report named cue N-1. next() computes "the cue after N-1" = N and seeks backwards to N's start — the tap looks dead. previous() 2.5 s into a cue can also step back one instead of restarting it.
    Fix: read _audioNavigator?.currentLocation (or playback.time) for time instead of parsing audioLocator's fragment, matching Android.
  3. Android: the whole new skipToCue path has no test (SyncAudiobookNavigator.kt:127-178, ReadiumReader.kt:1522-1535) confirmed; test → test
    Problem: the Kotlin unit tests cover only the pure cueStepTarget table. Untested: the playback.value read, super.goToLocator, markCueSynced's replacement of the flow's distinctUntilChangedBy, the manual-mode re-enable, and play-state preservation (criterion 4). The author states Android integration tests were not run.
    Regression that gets through: if the target audio href stops matching a reading-order item, AudiobookNavigator.goToLocator only logs and returns (AudiobookNavigator.kt:505-511) while skipToCue still returns true, so next() becomes a silent no-op and nothing fails. Same if a future change pauses the player around the seek.
    Fix: run the existing timebased_playback_test.dart cue-step cases on an emulator (they are already written and skip: kIsWeb), and add one unit test that a step does not emit narrationSyncChanged(true) twice.
  4. Web: the step reads its file from one source and its time from another (ReadiumReader.ts:1108-1114) plausible; correctness → test
    Problem: current is nav.currentLocator, but while playing time comes from nav.currentTime. The code's own comment says the locator "can still name the previous cue" — and after an automatic track change, it names the previous file while the clock already belongs to the new one.
    Failure case: the last cue of chapter 1 ends and chapter 2 starts; within one poll the locator href is still chap1.mp3 and currentTime is ~0.5. findCueStepTarget then anchors in chapter 1's cue list at 0.5 s → next() jumps back into chapter 1.
    Fix: settle it with a test first (lag the locator's href to the previous file, keep the clock in the next one). If it reproduces, take the href from the same snapshot as the time rather than mixing the two.

Nice to have

  1. Web: restartThresholdSecs is dead config (syncNarration.ts:372-381) → skip
    No production caller passes it — ReadiumReader.ts:1114 passes five arguments, and only syncNarration.test.ts:771 uses the sixth. iOS and Android keep the same value as a private constant. Delete the parameter and that test line; -2 lines.
  2. Web: this diff adds a third copy of the same lookup predicate (ReadiumReader.ts:1103-1104, already at :318-320 and :923-925) → skip
    Three identical !!this._audioNavPublication && detectSyncNarration(...) && !detectGuidedNavigation(...) blocks that must now change together; one private getter would do. It also re-walks the reading order on every keypress.
  3. iOS matches audio hrefs with raw string equality (FlutterMediaOverlay.swift:325,328,334) → skip
    Android uses Url.isEquivalent (FlutterMediaOverlay.kt:311) and web normalizeHref (syncNarration.ts:378). It follows existing iOS precedent (FlutterMediaOverlay.swift:38), so it is consistent, but a leading slash or a percent-encoded difference here makes every step a silent no-op on iOS only.

ddfreiling and others added 2 commits November 23, 2026 01:20
Cue steps were serialised against each other only. goToLocator and
goToProgression also reach goBounded, so a seek during a step could
run two go(to:) calls at once, and the first to finish cleared
_isNavigating while the other was still in flight. goBounded now
runs through one queue on the navigator, and a cue step runs its
read, seek and view resync inside it.

iOS also stepped from audioLocator, which only moves on the
delegate's periodic tick. It now reads playbackInfo, as Android and
web read their live position.

On Android, skipToCue returns false when the target cue's audio is
not in the reading order, instead of reporting a step that
goToLocator silently dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The navigator's current-locator flow is throttled and deduplicated.
After goToLocator, which writes its target into the timebased state,
a step back to a position the flow already reported (e.g. "previous"
restarting the cue the player sat on) produced no new emission. The
state then kept the goToLocator target instead of the cue.

skipToCue now returns the cue's locator, and the step writes it to the
state the same way goToLocator does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ddfreiling
ddfreiling requested review from m-abs and a balanced review from Copilot November 23, 2026 10:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved cue-step sequencing and session-isolation bugs can repeat cues, desynchronize narration and visuals, or apply queued steps to a different book.

8 open issues
What changed in this PR

This PR adds cue-by-cue narration stepping to media-overlay playback across iOS, Android, and web, without changing the public Dart API.

Changes:

  • Adds publication-wide cue selection, including chapter transitions and the two-second restart rule for previous().
  • Introduces queued stepping and narration-sync handling intended to preserve playback state.
  • Adds unit and integration coverage, API documentation, and a changelog fragment.
File Description
flutter_readium/​web/​src/​ReadiumReader.ts Adds queued cue stepping and sync handling.
flutter_readium/​web/​src/​navigators/​FlutterDivinaNavigator.ts Exposes comic manual-override state.
flutter_readium/​web/​src/​mediaoverlay/​syncNarration.ts Adds cue-target selection.
flutter_readium/​web/​src/​__tests__/​syncNarration.test.ts Tests cue boundaries and selection rules.
flutter_readium/​web/​src/​__tests__/​ReadiumReaderCueStep.test.ts Tests stepping, playback state, and synchronization.
flutter_readium/​lib/​flutter_readium.dart Documents public next/previous behavior.
flutter_readium/​ios/​flutter_readium/​Sources/​flutter_readium/​reader/​EPUBReaderView+Navigation.swift Adds target-specific narration resync.
flutter_readium/​ios/​flutter_readium/​Sources/​flutter_readium/​navigator/​FlutterMediaOverlayNavigator.swift Implements cue stepping and cue reporting.
flutter_readium/​ios/​flutter_readium/​Sources/​flutter_readium/​navigator/​FlutterAudioNavigator.swift Adds navigation serialization and live playhead lookup.
flutter_readium/​ios/​flutter_readium/​Sources/​flutter_readium/​model/​FlutterMediaOverlay.swift Adds publication-wide cue selection.
flutter_readium/​ios/​flutter_readium/​Sources/​flutter_readium/​FlutterReadiumPlugin.swift Routes media-overlay commands to cue stepping.
flutter_readium/​example/​ios/​RunnerTests/​FlutterMediaOverlayTests.swift Tests iOS cue-selection rules.
flutter_readium/​example/​integration_test/​groups/​timebased_playback_test.dart Adds native playback and synchronization scenarios.
flutter_readium/​changelog.d/​+media-overlay-cue-step.changed.md Describes the consumer-visible behavior change.
flutter_readium/​android/​src/​test/​kotlin/​dk/​nota/​flutterreadium/​models/​FlutterMediaOverlayTest.kt Tests Android cue-selection rules.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​ReadiumReader.kt Serializes cue commands and updates reader state.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​navigators/​SyncAudiobookNavigator.kt Implements cue seeking and explicit visual sync.
flutter_readium/​android/​src/​main/​kotlin/​dk/​nota/​flutterreadium/​models/​FlutterMediaOverlay.kt Adds publication-wide cue selection.
flutter_readium_platform_interface/​lib/​flutter_readium_platform_interface.dart Documents cue stepping versus time seeking.
docs/​api-reference/​flutter-readium.md Explains playback-unit behavior across platforms.

Review effort: Balanced

@ddfreiling

Copy link
Copy Markdown
Member Author

Thanks, worked through all seven.

  1. Fixed. Serialisation moved into the navigator: goBounded now runs through one serialized(_:) queue, so goToLocator/goToProgression and cue steps can no longer overlap go(to:). A cue step runs its whole read-position → seek → view-resync inside the queue (via goBoundedUnqueued), so a quick second step still starts from where the first landed. cueStepTask/enqueueCueStep are gone from the plugin.
  2. Fixed. iOS now steps from the player's live playbackInfo (resource index + time), matching Android and web. It falls back to audioLocator only while a timed-out go(to:) is still in flight, where reading playbackInfo can deadlock.
  3. Partly. The Android cue-step integration tests now run in CI and pass on API 24 and API 36 at ba298a3e (next and previous step one narrated cue and keep the paused state, next steps one narrated cue and keeps playing). The first asserts that narrationSyncChanged emits [true] exactly once. Running them caught one real bug, fixed in ba298a3e: after goToLocator, a previous that restarted the cue the player was already on produced no locator emission (the flow is throttled and deduplicated), so the timebased state kept the goToLocator target. A cue step now writes the cue's locator to state, as goToLocator does. I also added the guard you described: skipToCue reports no move when the target cue's audio is not in the reading order. No JVM unit test for the step itself; it needs a live AudioNavigator, and the integration test covers it.
  4. No change. It does not reproduce against ts-toolkit 2.13.2: AudioNavigator.go() calls pool.setCurrentAudio(trackIndex) and assigns currentLocation (with the new href) in the same synchronous block, and nextTrack() goes through go(). The position poll only replaces locations, never the href. So href and currentTime always refer to the same file; only the time can lag, which is what the live-clock read handles. A test that lags the href would only exercise our fake.
    5–7. Left as is, per your "skip".

🤖 Addressed by Claude Code

ddfreiling and others added 2 commits November 23, 2026 13:03
Addresses review feedback on the cue-step change:

- android: notification seek buttons seek by the interval again
  instead of stepping a cue. A step reads the position from the
  Media3 player, which reports a pending seek at once, and awaits the
  view catch-up out of manual mode while holding the step lock.
- ios: resyncToCue awaits the replay, so the serialized queue holds
  the next step until the view shows this one.
- web: queued steps are bound to the navigator active at the tap, and
  a new navigator starts a fresh chain. A step reads the live clock
  while paused too, stops when the audio seek fails, and samples the
  manual flags before the seek so sync emits a single true.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o feat/media-overlay-cue-step

* 'main' of https://github.com/Notalib/flutter_readium: (110 commits)
  fix(web): ignore reader keys pressed with Shift
  chore(doctor): say the fsmonitor daemon is off, not the setting
  fix(web): leave Space to DOM buttons and share reader key teardown
  docs(troubleshooting): run fsmonitor override from the example app
  test(android): assert every state after a paused goToLocator has progressions
  fix(android): publish upstream's landed locator after goToLocator
  fix(web): stop TTS when the speech engine ends without starting
  fix(android): narrow goToLocator result claims and test the false paths
  fix(web): turn one page per Space press, back on Shift+Space
  fix(web): ignore reader key shortcuts meant for the host app
  chore(release): v0.10.2
  fix(android): fill audio locator progression after goToLocator
  chore(doctor): fail when git core.fsmonitor is enabled on macOS
  fix(ios): open publications from file:// URL strings
  fix(web): zoom to the panel of a comic cue that loaded its chapter
  fix(android): percent-encode file paths when opening a publication
  fix(android): return navigation result from goToLocator
  fix(web): keep the newer book open when a superseded open fails
  fix(web): discard visual navigators from a superseded open
  fix(web): skip destroyed frames when reaching reader windows
  ...

# Conflicts:
#	flutter_readium/android/src/main/kotlin/dk/nota/flutterreadium/ReadiumReader.kt

This branch has not been deployed

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

Labels

low-priority This issue or PR is of low priority, do not take it first.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants