Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[video_player] Ensure seekTo is not called before video player is initialized. #4300

Merged
merged 15 commits into from
Sep 2, 2021
Merged
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
3 changes: 2 additions & 1 deletion packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.1.15

* Ensured seekTo isn't called before video player is initialized. Fixes [#89259](https://github.com/flutter/flutter/issues/89259).
* Updated Android lint settings.

## 2.1.14
Expand Down
12 changes: 7 additions & 5 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
}

Future<void> _applyLooping() async {
if (!value.isInitialized || _isDisposed) {
if (_isDisposedOrNotInitialized) {
return;
}
await _videoPlayerPlatform.setLooping(_textureId, value.isLooping);
}

Future<void> _applyPlayPause() async {
if (!value.isInitialized || _isDisposed) {
if (_isDisposedOrNotInitialized) {
return;
}
if (value.isPlaying) {
Expand Down Expand Up @@ -455,14 +455,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
}

Future<void> _applyVolume() async {
if (!value.isInitialized || _isDisposed) {
if (_isDisposedOrNotInitialized) {
return;
}
await _videoPlayerPlatform.setVolume(_textureId, value.volume);
}

Future<void> _applyPlaybackSpeed() async {
if (!value.isInitialized || _isDisposed) {
if (_isDisposedOrNotInitialized) {
return;
}

Expand Down Expand Up @@ -491,7 +491,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
/// If [moment] is outside of the video's full range it will be automatically
/// and silently clamped.
Future<void> seekTo(Duration position) async {
if (_isDisposed) {
if (_isDisposedOrNotInitialized) {
return;
}
if (position > value.duration) {
Expand Down Expand Up @@ -572,6 +572,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
value = value.copyWith(position: position);
value = value.copyWith(caption: _getCaptionAt(position));
}

bool get _isDisposedOrNotInitialized => _isDisposed || !value.isInitialized;
}

class _VideoAppLifeCycleObserver extends Object with WidgetsBindingObserver {
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for displaying inline video with other Flutter
widgets on Android, iOS, and web.
repository: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
version: 2.1.14
version: 2.1.15

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
22 changes: 22 additions & 0 deletions packages/video_player/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ void main() {
expect(fakeVideoPlayerPlatform.calls.last, 'setPlaybackSpeed');
});

test('play before initialized does not call platform', () async {
final VideoPlayerController controller = VideoPlayerController.network(
'https://127.0.0.1',
);
expect(controller.value.isInitialized, isFalse);

await controller.play();

expect(fakeVideoPlayerPlatform.calls, isEmpty);
});

test('play restarts from beginning if video is at end', () async {
final VideoPlayerController controller = VideoPlayerController.network(
'https://127.0.0.1',
Expand Down Expand Up @@ -373,6 +384,17 @@ void main() {
expect(await controller.position, const Duration(milliseconds: 500));
});

test('before initialized does not call platform', () async {
final VideoPlayerController controller = VideoPlayerController.network(
'https://127.0.0.1',
);
expect(controller.value.isInitialized, isFalse);

await controller.seekTo(const Duration(milliseconds: 500));

expect(fakeVideoPlayerPlatform.calls, isEmpty);
});

test('clamps values that are too high or low', () async {
final VideoPlayerController controller = VideoPlayerController.network(
'https://127.0.0.1',
Expand Down