Skip to content

Keep audio-session active while AudioNavigator moves to next resource - #945

Open
ddfreiling wants to merge 3 commits into
readium:developfrom
Notalib:fix/audio-session-kept-at-resource-boundary
Open

ddfreiling wants to merge 3 commits into
readium:developfrom
Notalib:fix/audio-session-kept-at-resource-boundary

Conversation

@ddfreiling

Copy link
Copy Markdown
Contributor

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. AudioSession deactivates the session whenever its user reports a pause in the background. To move on to the next resource, the AudioNavigator pauses 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 raw timeControlStatus. 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

  • Reporting: reportPlaybackToAudioSession() reports isPlaybackRequested from the rate change notification, the timeControlStatus observer, pause() and the end-of-item handler.
  • End of a resource: the player pauses itself at the end of an item before AVPlayerItemDidPlayToEndTime is handled. That pause is not reported; the end-of-item handler reports the playback once navigator(_:shouldPlayNextResource:) has answered.
  • Held at the end: a playback held at the end of a resource by the delegate, or ended at the end of the publication, now deactivates the session in the background. Before, the player paused there without a rate change notification, so the session was never told and stayed active.

Review notes

  • The end-of-item guard (hasPlayedToEndOfItemUnhandled) is needed: without it, playOnToNextResource failed 1 time in 3, when the timeControlStatus observer 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.
  • AudioSession is unchanged. Repeated reports of the same value are ignored there.

Verification

  • New AudioNavigatorTests suite "Audio session at the end of a resource", with a recording AudioSessionManaging: 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.
  • The 3 new tests need actual playback (.requiresPlayback), so they are skipped on the CI like the existing ones.
  • Full test suite passes on the iPad (A16) simulator, iOS 27.2. AudioNavigatorTests passed 10 times in a row. SwiftFormat lint is clean on the changed files.
  • Device: the fix itself was not run on a device. On an iPhone 13 Pro with the screen locked, a 3.11 build with deactivation disabled at the boundary kept the Now Playing controls across 3 boundaries, while an unchanged build lost them at the first boundary.

Not covered

  • The end of an item is detected with 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 the timeControlStatus observer runs first, which is the behaviour before this change.
  • No test covers pause() during a seek, which is the case the explicit report in pause() handles.

🤖 Generated with Claude Code

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>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 01:54
@ddfreiling ddfreiling changed the title Keep the audio session active when the AudioNavigator moves on to the next resource Keep audio-session active while AudioNavigator moves to next resource Nov 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

@mickael-menu

Copy link
Copy Markdown
Member

@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 develop? Thank you.

@ddfreiling

ddfreiling commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@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 develop? Thank you.

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.

ddfreiling and others added 2 commits November 18, 2026 23:28
…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>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants