Skip to content

Background audio playback with screen off #30

Description

@midasvo

Problem / Context

When the screen turns off or the player activity is backgrounded, playback pauses. Users want audio (e.g. music videos, concerts, "listening" to a series in bed) to keep playing with the screen off, with auto-advance to the next episode still working.

Upstream reference: jarnedemeulemeester#1206

Current behavior in our fork

  • app/phone/src/main/java/dev/jdtech/jellyfin/BasePlayerActivity.kt:43-53onPause() saves playWhenReady then forces viewModel.player.playWhenReady = false, pausing on any backgrounding including screen-off.
  • A MediaSession is built in onStart (:29) but released in onStop (:55-58); there is no foreground MediaSessionService, so the OS can kill playback and no playback notification exists.

Proposed implementation

  1. Add a media3 MediaSessionService (new class, e.g. player/local/.../PlaybackService.kt extending androidx.media3.session.MediaSessionService) that owns/holds the ExoPlayer and exposes a MediaSession. Register it in app/phone/src/main/AndroidManifest.xml with the androidx.media3.session.MediaSessionService intent filter and FOREGROUND_SERVICE_MEDIA_PLAYBACK permission.
  2. Settings key: add playerBackgroundAudio: Boolean (default false) to settings/.../domain/AppPreferences.kt (+ Constants.kt key) and a toggle in the player settings screen (settings module State/Action/ViewModel + app/phone/.../presentation/settings).
  3. Gate the force-pause: in BasePlayerActivity.onPause, only force playWhenReady = false when the background-audio setting is disabled.
  4. Keep the session alive in background: adjust MediaSession lifecycle so it is not released while audio should continue; rely on the foreground-service notification to keep the process.
  5. Consider detaching the video surface when backgrounded (audio-only continuation) to avoid wasted decoding.

MVI / settings pieces

  • AppPreferences.playerBackgroundAudio (DataStore boolean).
  • Settings screen: SettingsAction.SetBackgroundAudio, state field, and a SettingsSwitchCard.

Acceptance criteria

  • With background-audio enabled, turning the screen off keeps audio playing.
  • A media notification with play/pause/seek controls appears and controls the session.
  • Auto-advance to the next episode still works with the screen off.
  • With the setting disabled, behavior is unchanged (pauses on screen-off).
  • Returning to the app resumes the existing session without restarting playback.
  • Progress reporting (postPlaybackProgress/postPlaybackStop) still fires correctly.
  • FOREGROUND_SERVICE_MEDIA_PLAYBACK permission declared; builds on the targeted API levels.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestupstream-portFeature/fix ported or inspired by upstream jarnedemeulemeester/findroid

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions