Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.11.1

* Implements screen auto-lock control for video playback.
* Optimizes caption retrieval with binary search.

## 2.11.0
Expand Down
5 changes: 5 additions & 0 deletions packages/video_player/video_player/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ flutter:
- assets/bumble_bee_captions.srt
- assets/bumble_bee_captions.vtt
- assets/Audio.mp3
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
dependency_overrides:
video_player_avfoundation: {path: ../../../../packages/video_player/video_player_avfoundation}
video_player_platform_interface: {path: ../../../../packages/video_player/video_player_platform_interface}
91 changes: 84 additions & 7 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class VideoPlayerValue {
this.rotationCorrection = 0,
this.errorDescription,
this.isCompleted = false,
this.preventsDisplaySleepDuringVideoPlayback = true,
});

/// Returns an instance for a video that hasn't been loaded.
Expand Down Expand Up @@ -239,6 +240,12 @@ class VideoPlayerValue {
/// Does not update if video is looping.
final bool isCompleted;

/// Set this to true to prevent the screen from sleeping during video playback.
/// The default value is true.
///
/// This feature is currently only supported on iOS and macOS.
final bool preventsDisplaySleepDuringVideoPlayback;

/// The [size] of the currently loaded video.
final Size size;

Expand Down Expand Up @@ -287,6 +294,7 @@ class VideoPlayerValue {
int? rotationCorrection,
String? errorDescription = _defaultErrorDescription,
bool? isCompleted,
bool? preventsDisplaySleepDuringVideoPlayback,
}) {
return VideoPlayerValue(
duration: duration ?? this.duration,
Expand All @@ -306,6 +314,9 @@ class VideoPlayerValue {
? errorDescription
: this.errorDescription,
isCompleted: isCompleted ?? this.isCompleted,
preventsDisplaySleepDuringVideoPlayback:
preventsDisplaySleepDuringVideoPlayback ??
this.preventsDisplaySleepDuringVideoPlayback,
);
}

Expand All @@ -325,7 +336,8 @@ class VideoPlayerValue {
'volume: $volume, '
'playbackSpeed: $playbackSpeed, '
'errorDescription: $errorDescription, '
'isCompleted: $isCompleted),';
'isCompleted: $isCompleted, '
'preventsDisplaySleepDuringVideoPlayback: $preventsDisplaySleepDuringVideoPlayback),';
}

@override
Expand All @@ -347,7 +359,9 @@ class VideoPlayerValue {
size == other.size &&
rotationCorrection == other.rotationCorrection &&
isInitialized == other.isInitialized &&
isCompleted == other.isCompleted;
isCompleted == other.isCompleted &&
preventsDisplaySleepDuringVideoPlayback ==
other.preventsDisplaySleepDuringVideoPlayback;

@override
int get hashCode => Object.hash(
Expand All @@ -366,6 +380,7 @@ class VideoPlayerValue {
rotationCorrection,
isInitialized,
isCompleted,
preventsDisplaySleepDuringVideoPlayback,
);
}

Expand Down Expand Up @@ -399,7 +414,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
dataSourceType = platform_interface.DataSourceType.asset,
formatHint = null,
httpHeaders = const <String, String>{},
super(const VideoPlayerValue(duration: Duration.zero));
super(
VideoPlayerValue(
duration: Duration.zero,
preventsDisplaySleepDuringVideoPlayback:
videoPlayerOptions?.preventsDisplaySleepDuringVideoPlayback ??
true,
),
);

/// Constructs a [VideoPlayerController] playing a network video.
///
Expand All @@ -425,7 +447,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
}) : _closedCaptionFileFuture = closedCaptionFile,
dataSourceType = platform_interface.DataSourceType.network,
package = null,
super(const VideoPlayerValue(duration: Duration.zero));
super(
VideoPlayerValue(
duration: Duration.zero,
preventsDisplaySleepDuringVideoPlayback:
videoPlayerOptions?.preventsDisplaySleepDuringVideoPlayback ??
true,
),
);

/// Constructs a [VideoPlayerController] playing a network video.
///
Expand All @@ -447,7 +476,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
dataSource = url.toString(),
dataSourceType = platform_interface.DataSourceType.network,
package = null,
super(const VideoPlayerValue(duration: Duration.zero));
super(
VideoPlayerValue(
duration: Duration.zero,
preventsDisplaySleepDuringVideoPlayback:
videoPlayerOptions?.preventsDisplaySleepDuringVideoPlayback ??
true,
),
);

/// Constructs a [VideoPlayerController] playing a video from a file.
///
Expand All @@ -464,7 +500,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
dataSourceType = platform_interface.DataSourceType.file,
package = null,
formatHint = null,
super(const VideoPlayerValue(duration: Duration.zero));
super(
VideoPlayerValue(
duration: Duration.zero,
preventsDisplaySleepDuringVideoPlayback:
videoPlayerOptions?.preventsDisplaySleepDuringVideoPlayback ??
true,
),
);

/// Constructs a [VideoPlayerController] playing a video from a contentUri.
///
Expand All @@ -485,7 +528,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
package = null,
formatHint = null,
httpHeaders = const <String, String>{},
super(const VideoPlayerValue(duration: Duration.zero));
super(
VideoPlayerValue(
duration: Duration.zero,
preventsDisplaySleepDuringVideoPlayback:
videoPlayerOptions?.preventsDisplaySleepDuringVideoPlayback ??
true,
),
);

/// The URI to the video file. This will be in different formats depending on
/// the [DataSourceType] of the original video.
Expand Down Expand Up @@ -590,6 +640,11 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
_creatingCompleter!.complete(null);
final initializingCompleter = Completer<void>();

await _videoPlayerPlatform.setPreventsDisplaySleepDuringVideoPlayback(
_playerId,
value.preventsDisplaySleepDuringVideoPlayback,
);

// Apply the web-specific options
if (kIsWeb && videoPlayerOptions?.webOptions != null) {
await _videoPlayerPlatform.setWebOptions(
Expand Down Expand Up @@ -715,6 +770,18 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
await _applyLooping();
}

/// Sets whether or not the screen should be prevented from sleeping during video playback.
/// See also [VideoPlayerValue.preventsDisplaySleepDuringVideoPlayback].
Future<void> setPreventsDisplaySleepDuringVideoPlayback(
bool preventsDisplaySleepDuringVideoPlayback,
) async {
value = value.copyWith(
preventsDisplaySleepDuringVideoPlayback:
preventsDisplaySleepDuringVideoPlayback,
);
await _applyPreventsDisplaySleepDuringVideoPlayback();
}

/// Pauses the video.
Future<void> pause() async {
value = value.copyWith(isPlaying: false);
Expand All @@ -728,6 +795,16 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
await _videoPlayerPlatform.setLooping(_playerId, value.isLooping);
}

Future<void> _applyPreventsDisplaySleepDuringVideoPlayback() async {
if (_isDisposedOrNotInitialized) {
return;
}
await _videoPlayerPlatform.setPreventsDisplaySleepDuringVideoPlayback(
_playerId,
value.preventsDisplaySleepDuringVideoPlayback,
);
}

Future<void> _applyPlayPause() async {
if (_isDisposedOrNotInitialized) {
return;
Expand Down
7 changes: 7 additions & 0 deletions packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ dev_dependencies:
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)

# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
dependency_overrides:
video_player_avfoundation: {path: ../../../packages/video_player/video_player_avfoundation}
video_player_platform_interface: {path: ../../../packages/video_player/video_player_platform_interface}

34 changes: 33 additions & 1 deletion packages/video_player/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class FakeController extends ValueNotifier<VideoPlayerValue>
@override
Future<void> setLooping(bool looping) async {}

@override
Future<void> setPreventsDisplaySleepDuringVideoPlayback(
bool prevents,
) async {}

@override
VideoFormat? get formatHint => null;

Expand Down Expand Up @@ -1858,7 +1863,8 @@ void main() {
'volume: 0.5, '
'playbackSpeed: 1.5, '
'errorDescription: null, '
'isCompleted: false),',
'isCompleted: false, '
'preventsDisplaySleepDuringVideoPlayback: true),',
);
});

Expand Down Expand Up @@ -1954,6 +1960,24 @@ void main() {
expect(controller.videoPlayerOptions!.mixWithOthers, true);
});

test('setPreventsDisplaySleepDuringVideoPlayback', () async {
final controller = VideoPlayerController.networkUrl(_localhostUri);
addTearDown(controller.dispose);

await controller.initialize();
expect(controller.value.preventsDisplaySleepDuringVideoPlayback, true);

await controller.setPreventsDisplaySleepDuringVideoPlayback(false);
expect(controller.value.preventsDisplaySleepDuringVideoPlayback, false);

expect(
fakeVideoPlayerPlatform.calls.contains(
'setPreventsDisplaySleepDuringVideoPlayback',
),
true,
);
});

test('true allowBackgroundPlayback continues playback', () async {
final controller = VideoPlayerController.networkUrl(
_localhostUri,
Expand Down Expand Up @@ -2227,6 +2251,14 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
calls.add('setMixWithOthers');
}

@override
Future<void> setPreventsDisplaySleepDuringVideoPlayback(
int playerId,
bool preventsDisplaySleepDuringVideoPlayback,
) async {
calls.add('setPreventsDisplaySleepDuringVideoPlayback');
}

@override
Widget buildView(int playerId) {
return Texture(textureId: playerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ flutter:
assets:
- assets/flutter-mark-square-64.png
- assets/Butterfly-209.mp4
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
dependency_overrides:
video_player_platform_interface: {path: ../../../../packages/video_player/video_player_platform_interface}
4 changes: 4 additions & 0 deletions packages/video_player/video_player_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ dev_dependencies:
topics:
- video
- video-player
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
dependency_overrides:
video_player_platform_interface: {path: ../../../packages/video_player/video_player_platform_interface}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.9.5

* Implements screen auto-lock control for video playback.

## 2.9.4

* Ensures that the display link does not continue requesting frames after a player is disposed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ - (void)selectAudioTrackAtIndex:(NSInteger)trackIndex
}
}

- (void)setPreventsDisplaySleepDuringVideoPlayback:(BOOL)preventsDisplaySleepDuringVideoPlayback error:(FlutterError *_Nullable *_Nonnull)error {
if (@available(iOS 12.0, macOS 10.14, *)) {
self.player.preventsDisplaySleepDuringVideoPlayback = preventsDisplaySleepDuringVideoPlayback;
}
}

#pragma mark - Private

- (int64_t)duration {
Expand Down
Loading