Repository navigation
Keep audio-session active while AudioNavigator moves to next resource - #945
ddfreiling wants to merge 3 commits into
Conversation
The AudioNavigator paused the player to jump to the next resource and reported that pause to AudioSession, which deactivates the session in the background. iOS then removed the Now Playing controls at the first resource boundary. The navigator now reports its playback intent (isPlaybackRequested) instead of the raw timeControlStatus. The pause the player makes by itself at the end of an item is reported by the end-of-item handler, once the delegate decided whether to move on. A playback held or ended at the end of a resource is now reported as paused, as the player pauses there without a rate change notification. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AudioNavigator moves on to the next resourceAudioNavigator moves to next resource
There was a problem hiding this comment.
🟡 Changes recommended
Explicit pauses after end-of-item seeks can leave the audio session marked as playing, and the test can assert before end-of-item handling finishes, causing intermittent failures.
2 open issues
What changed in this PR
Updates AudioNavigator to preserve Now Playing controls when background audiobook playback moves between resources, while releasing the audio session when playback stops.
Changes:
- Reports playback intent instead of temporary player pauses during seeks and resource transitions.
- Adds playback tests for automatic transitions, delegate-held stops, and publication completion.
- Documents the audio-session fixes in the changelog.
| File | Description |
|---|---|
Tests/NavigatorTests/RecordingAudioSessionManager.swift |
Records playback reports, ignoring consecutive duplicates. |
Tests/NavigatorTests/Audio/AudioNavigatorTests.swift |
Adds audio-session coverage for resource and publication boundaries. |
Sources/Navigator/Audiobook/AudioNavigator.swift |
Centralizes playback-intent reporting and defers automatic end-of-item pauses. |
CHANGELOG.md |
Adds unreleased notes for background audio-session behavior. |
Review effort: Balanced
|
@ddfreiling I cannot reproduce the issue by putting the app in the background, locking the device and waiting for the audio to cross a reading order resource. Would you mind sharing reproducing steps and maybe a screencast, alongside a faulty commit hash on |
Hey Mikael. The issue occurs if the device has poor internet connection and takes more than 2 sec to start playing the next track. This can be stimulated with the Network Link Conditioner on ios, set to Very Poor Network. We've had lots of reports of this in the wild and can repro it consistently. Can do a screen recording tmrw. |
…kept-at-resource-boundary * origin/develop: Resume the `AudioNavigator` by itself after a network stall (readium#944) # Conflicts: # CHANGELOG.md
The end-of-item guard of reportPlaybackToAudioSession() could drop a pause() made while the player sat at the end of an item, e.g. from didJumpTo after a jump to its end, leaving the audio session marked as playing in the background. The tests waited for the reported playback state, which the timeControlStatus observer can publish before the end-of-item handler reports the pause to the audio session. They now wait for that report. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary
When an audiobook plays in the background, the lock screen and Control Center lose their Now Playing controls at the first resource boundary, while the audio carries on.
AudioSessiondeactivates the session whenever its user reports a pause in the background. To move on to the next resource, theAudioNavigatorpauses the player for the jump and reports that pause, so the session is deactivated and activated again about 50 ms later. iOS removes the Now Playing controls when the session is deactivated.The navigator now reports its playback intent (
isPlaybackRequested) to the audio session, instead of the rawtimeControlStatus. A pause during a seek or a jump that resumes the playback is no longer a pause for the audio session. A pause by the user or the app, in the foreground or in the background, still deactivates it as before.Change
reportPlaybackToAudioSession()reportsisPlaybackRequestedfrom the rate change notification, thetimeControlStatusobserver,pause()and the end-of-item handler.AVPlayerItemDidPlayToEndTimeis handled. That pause is not reported; the end-of-item handler reports the playback oncenavigator(_:shouldPlayNextResource:)has answered.Review notes
hasPlayedToEndOfItemUnhandled) is needed: without it,playOnToNextResourcefailed 1 time in 3, when thetimeControlStatusobserver ran before the end-of-item handler.pause()reports explicitly, because pausing a player that is already paused for a seek doesn't change its rate.AudioSessionis unchanged. Repeated reports of the same value are ignored there.Verification
AudioNavigatorTestssuite "Audio session at the end of a resource", with a recordingAudioSessionManaging: playing on to the next resource reports no pause; a playback held by the delegate reports one; playing up to the end of the publication reports one. Without the fix, all three fail..requiresPlayback), so they are skipped on the CI like the existing ones.AudioNavigatorTestspassed 10 times in a row. SwiftFormat lint is clean on the changed files.Not covered
currentTime() >= duration. An item ending before its reported duration (e.g. a VBR MP3 with an estimated duration) can still deactivate the session at that boundary if thetimeControlStatusobserver runs first, which is the behaviour before this change.pause()during a seek, which is the case the explicit report inpause()handles.🤖 Generated with Claude Code