Skip to content

fix(Android): Keep screen awake after returning to video playback from background - #1047

Merged
PartyDonut merged 2 commits into
DonutWare:developfrom
aviadlevy:fix/android-screen-sleep-during-playback
Aug 9, 2026
Merged

fix(Android): Keep screen awake after returning to video playback from background#1047
PartyDonut merged 2 commits 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.
@aviadlevy
aviadlevy force-pushed the fix/android-screen-sleep-during-playback branch from a3c8545 to 46ea3ff Compare August 9, 2026 13:49
PartyDonut
PartyDonut previously approved these changes Aug 9, 2026

@PartyDonut PartyDonut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me and works fine. One small request to remove some comments.

Comment thread lib/wrappers/media_control_wrapper.dart Outdated
Review feedback on DonutWare#1047: the helper names already say what they do, so
the doc comments on _wakelockEnabled, _shouldKeepScreenOn and
reassertWakelock were noise.

Keeps only the note on _applyWakelock's force flag, since "re-apply even
though the cached state matches" is not derivable from the code.

@PartyDonut PartyDonut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the quick changes 👍

@PartyDonut PartyDonut added the bug Something isn't working label Aug 9, 2026
@PartyDonut PartyDonut changed the title fix: keep screen awake after returning to video playback from background fix: Keep screen awake after returning to video playback from background Aug 9, 2026
@PartyDonut PartyDonut changed the title fix: Keep screen awake after returning to video playback from background fix(Android): Keep screen awake after returning to video playback from background Aug 9, 2026
@PartyDonut
PartyDonut merged commit 7303097 into DonutWare:develop Aug 9, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this to Done in Fladder Aug 9, 2026
@aviadlevy
aviadlevy deleted the fix/android-screen-sleep-during-playback branch August 9, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Screen sleeps during video playback after returning from background

2 participants