Skip to content

[video_player] Implement screen auto-lock control for video playback#11225

Open
shrabanti722 wants to merge 17 commits intoflutter:mainfrom
shrabanti722:video-player-screen-auto-lock
Open

[video_player] Implement screen auto-lock control for video playback#11225
shrabanti722 wants to merge 17 commits intoflutter:mainfrom
shrabanti722:video-player-screen-auto-lock

Conversation

@shrabanti722
Copy link
Copy Markdown

@shrabanti722 shrabanti722 commented Mar 11, 2026

Description

Adds support for screen auto-lock during video playback on iOS.

By default, the video player keeps the screen awake during playback. Some apps (e.g. login screens with background videos, ambient content) want the screen to be able to auto-lock while video plays. This PR adds an option to allow that behavior.

Changes

  • Add preventsDisplaySleepDuringVideoPlayback to VideoPlayerOptions (default: true to preserve existing behavior)
  • When false, the screen can auto-lock during playback
  • Implemented on iOS/macOS via AVPlayer.preventsDisplaySleepDuringVideoPlayback
  • Platform interface: setPreventsDisplaySleepDuringVideoPlayback(int playerId, bool preventsDisplaySleepDuringVideoPlayback)

Usage

VideoPlayerController.networkUrl(
  url,
  videoPlayerOptions: VideoPlayerOptions(preventsDisplaySleepDuringVideoPlayback: false),
);

Breakout PRs

  1. Platform interface: [video_player_platform_interface] Add preventsDisplaySleepDuringVideoPlayback option #11546
  2. AVFoundation (iOS/macOS): [video_player_avfoundation] Implement preventsDisplaySleepDuringVideoPlayback #11547 (draft)

Issues fixed

  • Fixes: flutter/flutter#183520 (How to disable wakelock on iOS side?)
  • Addresses the need to allow screen auto-lock during video playback on iOS (e.g. for ambient/background videos)

Pre-Review Checklist

- Added `setAllowScreenAutoLock` method to the video player platform interface and its implementations for iOS.
- Updated `VideoPlayerOptions` to include `allowScreenAutoLock` property.
- Modified the `AVFoundationVideoPlayer` class to handle screen auto-lock settings during playback.
- Added tests to verify the functionality of screen auto-lock settings.

This feature allows users to control whether the screen should stay awake during video playback on iOS devices.
- Removed redundant player ID assignment and completer initialization from the `VideoPlayerController` class.
- Enhanced code clarity by consolidating player setup logic.
- This change improves maintainability and readability of the video player implementation.
…ontrol for video playback in the video player packages.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Mar 11, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@shrabanti722 shrabanti722 changed the title Video player screen auto lock [video_player] Implement screen auto-lock control for video playback Mar 11, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new feature to control screen auto-locking during video playback by adding an allowScreenAutoLock option. The changes are implemented across the platform interface, the main video_player package, and the video_player_avfoundation implementation for iOS and macOS. My review has identified a few areas for improvement: a potential bug in the macOS implementation's availability check, a style guide violation regarding dependency_overrides in pubspec.yaml files, and opportunities for enhancing code clarity and documentation.

Comment thread packages/video_player/video_player/lib/video_player.dart Outdated
Comment thread packages/video_player/video_player/pubspec.yaml Outdated
Comment thread packages/video_player/video_player_avfoundation/pubspec.yaml
Copy link
Copy Markdown
Contributor

@LouiseHsu LouiseHsu left a comment

Choose a reason for hiding this comment

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

looks like you have some compilation errors.

https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8687630786392823857/+/u/Run_package_tests/drive_examples/stdout

../lib/video_player.dart:594:34: Error: The method 'setAllowScreenAutoLock' isn't defined for the type 'VideoPlayerPlatform'.
 - 'VideoPlayerPlatform' is from 'package:video_player_platform_interface/video_player_platform_interface.dart' ('../../../../../.pub-cache/hosted/pub.dev/video_player_platform_interface-6.6.0/lib/video_player_platform_interface.dart').
Try correcting the name to the name of an existing method, or defining a method named 'setAllowScreenAutoLock'.
      await _videoPlayerPlatform.setAllowScreenAutoLock(

@JetA2
Copy link
Copy Markdown

JetA2 commented Mar 12, 2026

Thanks for the PR, I was just looking at this issue.
For my use case, it would be preferable to have this feature on the VideoPlayerControllerinstead of VideoPlayerOptions, otherwise you have to create a new controller if you want to change the value of the setting.

In my use case I have a video playing in a list without sound ("background mode" that should allow the device to auto-lock), but when it's tapped will go to full screen with sound ("foreground mode" that should keep the device awake).

If I were to recreate the controller when moving to fullscreen, the playback would be interrupted.

…ckages to use relative paths for local dependencies. This change is for testing and initial review only; do not merge.
shrabanti722 and others added 3 commits March 14, 2026 17:12
…layer_avfoundation/Sources/video_player_avfoundation/FVPVideoPlayer.m

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…o_player_platform_interface.dart

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@LouiseHsu LouiseHsu added the CICD Run CI/CD label Mar 16, 2026
@mdebbar mdebbar removed their request for review March 18, 2026 16:05
@@ -1,3 +1,7 @@
## 2.9.5

* Implement screen auto-lock control for video playback.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Implements

Copy link
Copy Markdown
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

iOS part LGTM

@nateshmbhat
Copy link
Copy Markdown
Contributor

Thanks @shrabanti722 for this feature. we also need this option in our app badly : )

@LouiseHsu LouiseHsu added CICD Run CI/CD and removed CICD Run CI/CD labels Mar 31, 2026
@LouiseHsu
Copy link
Copy Markdown
Contributor

https://ci.chromium.org/ui/p/flutter/builders/try/Linux%20repo_checks/22937/overview

Can you make sure your changelog + pubspec is up to date? also make sure to run dart format!

https://ci.chromium.org/ui/p/flutter/builders/try/Mac_arm64%20ios_platform_tests_shard_1%20master/27862/overview

00:27 +10 ~1: /Volumes/Work/s/w/ir/x/w/packages/packages/video_player/video_player/example/integration_test/video_player_test.dart: network videos reports buffering status
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following PlatformException was thrown running a test:
PlatformException(VideoError, Failed to load video: unknown error: The operation couldn’t be
completed. (CoreMediaErrorDomain error -16845 - HTTP 429: (unhandled)), null, null)

Copy link
Copy Markdown
Contributor

@tarrinneal tarrinneal left a comment

Choose a reason for hiding this comment

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

A couple changelog adjustments and doc comment changes, This should be good to break out into child pr's.

@@ -1,5 +1,6 @@
## 2.11.1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will need to be a new entry in the changelog along with a version bump to 2.12.0

@@ -1,5 +1,6 @@
## NEXT
## 6.6.1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

6.7.0

# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 6.6.0
version: 6.6.1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

6.7.0

/// currently no way to implement this feature in this platform).
final bool mixWithOthers;

/// Set this to true to prevent the screen from sleeping during video playback.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here and everywhere else this comment is located:

"Whether the screen is prevented from from sleeping during video playback."

https://dart.dev/effective-dart/documentation#prefer-starting-a-boolean-variable-or-property-comment-with-whether-followed-by-a-noun-or-gerund-phrase

final bool mixWithOthers;

/// Set this to true to prevent the screen from sleeping during video playback.
/// The default value is true.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There should be an empty line between this and the line above it. (for all versions of this comment

@@ -1,3 +1,7 @@
## 2.9.5
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2.10.0

@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 4, 2026
@shrabanti722 shrabanti722 requested a review from tarrinneal April 4, 2026 07:55
# the parent directory to use the current plugin's version.
path: ../
video_player_platform_interface: ^6.6.0
video_player_platform_interface: ^6.7.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

revert this

flutter:
sdk: flutter
video_player_platform_interface: ^6.6.0
video_player_platform_interface: ^6.7.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and this

# the parent directory to use the current plugin's version.
path: ../
video_player_platform_interface: ^6.6.0
video_player_platform_interface: ^6.7.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and this

flutter:
sdk: flutter
video_player_platform_interface: ^6.3.0
video_player_platform_interface: ^6.7.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and this

flutter_web_plugins:
sdk: flutter
video_player_platform_interface: ^6.4.0
video_player_platform_interface: ^6.7.0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and this

@shrabanti722 shrabanti722 requested a review from tarrinneal April 7, 2026 07:42
@LouiseHsu LouiseHsu added the CICD Run CI/CD label Apr 7, 2026
@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 8, 2026
@LouiseHsu LouiseHsu added the CICD Run CI/CD label Apr 15, 2026
@shrabanti722
Copy link
Copy Markdown
Author

The tests are failing due to version issues. How to proceed forward ? Should I create a child PR with the version updates ? @LouiseHsu @tarrinneal

shrabanti722 added a commit to shrabanti722/packages that referenced this pull request Apr 21, 2026
…Playback option

Adds the `preventsDisplaySleepDuringVideoPlayback` field to
`VideoPlayerOptions` and a `setPreventsDisplaySleepDuringVideoPlayback`
method to `VideoPlayerPlatform`, allowing platform implementations to
control whether the display sleeps during video playback.

Platform interface breakout PR for flutter#11225.

Made-with: Cursor
shrabanti722 added a commit to shrabanti722/packages that referenced this pull request Apr 21, 2026
…Playback option

Adds the `preventsDisplaySleepDuringVideoPlayback` field to
`VideoPlayerOptions` and a `setPreventsDisplaySleepDuringVideoPlayback`
method to `VideoPlayerPlatform`, allowing platform implementations to
control whether the display sleeps during video playback.

Platform interface breakout PR for flutter#11225.

Made-with: Cursor
shrabanti722 added a commit to shrabanti722/packages that referenced this pull request Apr 21, 2026
…Playback

Implements the platform interface method
`setPreventsDisplaySleepDuringVideoPlayback` using
`AVPlayer.preventsDisplaySleepDuringVideoPlayback` on iOS and macOS.

AVFoundation implementation breakout PR for flutter#11225.
Depends on the platform interface breakout PR (6.7.0).

Made-with: Cursor
@shrabanti722
Copy link
Copy Markdown
Author

Hi @tarrinneal @LouiseHsu — I've split this into child PRs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[video_player] Allow screen auto-lock during video playback on iOS

7 participants