Skip to content

fix: keep screen awake after returning to video playback from background#1047

Open
aviadlevy wants to merge 1 commit into
DonutWare:developfrom
aviadlevy:fix/android-screen-sleep-during-playback
Open

fix: keep screen awake after returning to video playback from background#1047
aviadlevy wants to merge 1 commit into
DonutWare:developfrom
aviadlevy:fix/android-screen-sleep-during-playback

Conversation

@aviadlevy

@aviadlevy aviadlevy commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Pull Request Description

Fixes the screen sleeping mid-playback on Android (see #1046 for the full repro and on-device evidence).

Root cause: screen-on relied on the FLAG_KEEP_SCREEN_ON window flag toggled once in play()/pause(). Android drops that flag across a background→foreground cycle and nothing re-applied it — the resume handler meant to recover it (if (playing) play()) was dead code, since playing was never set true.

Fix

  • media_control_wrapper.dart: route the wakelock through one idempotent path — _shouldKeepScreenOn(playing) (on when playing and not an AudioModel) applied via _applyWakelock(...), re-evaluated on every player state change instead of the scattered WakelockPlus calls.
  • Added reassertWakelock() which force-re-applies the flag (our cached state reads "on" after the OS clears it, so a plain reconcile would skip the re-apply).
  • video_player.dart: removed the dead playing flag; call reassertWakelock() on AppLifecycleState.resumed. Desktop/web stay excluded, as before.

The same handler also had a dead pause()-on-background branch (also gated on the never-true playing), which is removed too — behavior is unchanged, since playback already continued in the background. Wiring up actual background-pause would be a separate change.

Scope

Native ExoPlayer path (Android TV) manages the flag separately in ExoPlayer.kt and is out of scope. No new packages.

AI assistance

Root-cause investigation and the patch were done with Claude Code; diagnosis, fix design, and on-device validation were human-reviewed and -owned.

Issue Being Fixed

Resolves #1046

Screenshots / Recordings

Tested On

  • Android — dev build on a physical Pixel 10 Pro XL (Android 17): reproduced the mid-playback sleep via a background→foreground cycle, then confirmed the keep-screen-on flag stays held and the screen stays awake through the idle timeout while the video keeps playing.
  • Android TV — native ExoPlayer path, out of scope.
  • iOS
  • Linux — same LibMPV/WakelockPlus path as Android; not separately re-tested.
  • Windows
  • macOS
  • Web

Checklist

  • If a new package was added, did you ensure it works for all supported platforms? Is the package well maintained — no new packages added.
  • Check that any changes are related to the issue at hand.

On Android the keep-screen-on window flag (FLAG_KEEP_SCREEN_ON) is dropped
across a background→foreground cycle, so the screen would sleep after the
idle timeout while a video kept playing. The wakelock was only set in play()
and cleared in pause() (set-and-forget), and the lifecycle resume handler
meant to re-apply it was dead code: its `playing` flag was never set to true.

Route the wakelock through a single idempotent path in MediaControlsWrapper
driven by the actual player state (on when playing and not an AudioModel),
and add reassertWakelock() which force re-applies it. Call it from
AppLifecycleState.resumed so the flag is restored whenever the app returns
to the foreground while playback continues.
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.

Screen sleeps during video playback after returning from background

1 participant