Skip to content

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

Open
shrabanti722 wants to merge 7 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 7 commits intoflutter:mainfrom
shrabanti722:video-player-screen-auto-lock

Conversation

@shrabanti722
Copy link

@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 allowScreenAutoLock to VideoPlayerOptions (default: false to keep current behavior)
  • When true, the screen can auto-lock during playback
  • Implemented on iOS via AVPlayer.preventsDisplaySleepDuringVideoPlayback
  • Platform interface: setAllowScreenAutoLock(int playerId, bool allowScreenAutoLock)

Usage

dart
VideoPlayerController.networkUrl(
url,
videoPlayerOptions: VideoPlayerOptions(allowScreenAutoLock: true),
);

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

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

@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 on lines +512 to +514
if (@available(iOS 12.0, *)) {
self.player.preventsDisplaySleepDuringVideoPlayback = !allowScreenAutoLock;
}

Choose a reason for hiding this comment

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

high

The availability check @available(iOS 12.0, *) only considers iOS. Since this package also supports macOS and preventsDisplaySleepDuringVideoPlayback is available on macOS 10.14+, the check should be updated to include macOS to ensure this feature works correctly on that platform.

Suggested change
if (@available(iOS 12.0, *)) {
self.player.preventsDisplaySleepDuringVideoPlayback = !allowScreenAutoLock;
}
if (@available(iOS 12.0, macOS 10.14, *)) {
self.player.preventsDisplaySleepDuringVideoPlayback = !allowScreenAutoLock;
}

Comment on lines 590 to +595
_creatingCompleter!.complete(null);
final initializingCompleter = Completer<void>();

if (videoPlayerOptions?.allowScreenAutoLock != null) {
await _videoPlayerPlatform.setAllowScreenAutoLock(
_playerId,

Choose a reason for hiding this comment

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

medium

The condition videoPlayerOptions?.allowScreenAutoLock != null is a bit confusing because allowScreenAutoLock is a non-nullable boolean in VideoPlayerOptions. This check is effectively the same as videoPlayerOptions != null. To improve clarity and make the intent clearer, it's better to check for videoPlayerOptions directly.

Suggested change
_creatingCompleter!.complete(null);
final initializingCompleter = Completer<void>();
if (videoPlayerOptions?.allowScreenAutoLock != null) {
await _videoPlayerPlatform.setAllowScreenAutoLock(
_playerId,
if (videoPlayerOptions != null) {
await _videoPlayerPlatform.setAllowScreenAutoLock(
_playerId,
videoPlayerOptions.allowScreenAutoLock,
);
}

topics:
- video
- video-player

Choose a reason for hiding this comment

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

medium

According to the repository style guide, temporary dependency_overrides should be accompanied by a comment to prevent them from being merged. Please add the comment # FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE. before the dependency_overrides: block.

References
  1. The repository style guide states that temporary dependency_overrides sections in pubspec.yaml files should include a comment starting with 'FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.' to prevent accidental merging. This override is missing the required comment. (link)

- video
- video-player

dependency_overrides:

Choose a reason for hiding this comment

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

medium

According to the repository style guide, temporary dependency_overrides should be accompanied by a comment to prevent them from being merged. Please add the comment # FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE. before the dependency_overrides: block.

References
  1. The repository style guide states that temporary dependency_overrides sections in pubspec.yaml files should include a comment starting with 'FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.' to prevent accidental merging. This override is missing the required comment. (link)

/// Set this to true to allow the screen to auto-lock during video playback.
/// The default value is false, meaning the screen will stay awake during playback.
///
/// This option is currently only supported on iOS.

Choose a reason for hiding this comment

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

medium

The documentation states that this option is currently only supported on iOS. However, the implementation is in the video_player_avfoundation package, which supports both iOS and macOS. With the suggested change in FVPVideoPlayer.m, this feature will also be available on macOS. The comment should be updated to reflect this.

Suggested change
/// This option is currently only supported on iOS.
/// This option is currently only supported on iOS and macOS.

Copy link
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

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.
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.

Allow screen auto-lock during video playback on iOS

3 participants