[video_player] Implement screen auto-lock control for video playback#11225
[video_player] Implement screen auto-lock control for video playback#11225shrabanti722 wants to merge 7 commits intoflutter:mainfrom
Conversation
- 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.
Made-with: Cursor
…ontrol for video playback in the video player packages.
|
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. |
There was a problem hiding this comment.
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.
| if (@available(iOS 12.0, *)) { | ||
| self.player.preventsDisplaySleepDuringVideoPlayback = !allowScreenAutoLock; | ||
| } |
There was a problem hiding this comment.
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.
| if (@available(iOS 12.0, *)) { | |
| self.player.preventsDisplaySleepDuringVideoPlayback = !allowScreenAutoLock; | |
| } | |
| if (@available(iOS 12.0, macOS 10.14, *)) { | |
| self.player.preventsDisplaySleepDuringVideoPlayback = !allowScreenAutoLock; | |
| } |
| _creatingCompleter!.complete(null); | ||
| final initializingCompleter = Completer<void>(); | ||
|
|
||
| if (videoPlayerOptions?.allowScreenAutoLock != null) { | ||
| await _videoPlayerPlatform.setAllowScreenAutoLock( | ||
| _playerId, |
There was a problem hiding this comment.
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.
| _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 | ||
|
|
There was a problem hiding this comment.
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
- The repository style guide states that temporary
dependency_overridessections inpubspec.yamlfiles 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: |
There was a problem hiding this comment.
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
- The repository style guide states that temporary
dependency_overridessections inpubspec.yamlfiles 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. |
There was a problem hiding this comment.
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.
| /// This option is currently only supported on iOS. | |
| /// This option is currently only supported on iOS and macOS. |
LouiseHsu
left a comment
There was a problem hiding this comment.
looks like you have some compilation errors.
../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(
|
Thanks for the PR, I was just looking at this issue. 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.
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
allowScreenAutoLocktoVideoPlayerOptions(default:falseto keep current behavior)true, the screen can auto-lock during playbackAVPlayer.preventsDisplaySleepDuringVideoPlaybacksetAllowScreenAutoLock(int playerId, bool allowScreenAutoLock)Usage
dart
VideoPlayerController.networkUrl(
url,
videoPlayerOptions: VideoPlayerOptions(allowScreenAutoLock: true),
);
Issues fixed
Pre-Review Checklist
[video_player]///).