Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
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
4 changes: 4 additions & 0 deletions packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.0

* Adds `allowBackgroundPlayback` to `VideoPlayerOptions`.

## 2.2.13

* Fixes persisting of hasError even after successful initialize.
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 @@ -17,6 +17,11 @@ dependencies:
# the parent directory to use the current plugin's version.
path: ../

dependency_overrides:
video_player_platform_interface:
path:
../../video_player_platform_interface

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
12 changes: 8 additions & 4 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
bool _isDisposed = false;
Completer<void>? _creatingCompleter;
StreamSubscription<dynamic>? _eventSubscription;
late _VideoAppLifeCycleObserver _lifeCycleObserver;
_VideoAppLifeCycleObserver? _lifeCycleObserver;

/// The id of a texture that hasn't been initialized.
@visibleForTesting
Expand All @@ -299,8 +299,12 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {

/// Attempts to open the given [dataSource] and load metadata about the video.
Future<void> initialize() async {
_lifeCycleObserver = _VideoAppLifeCycleObserver(this);
_lifeCycleObserver.initialize();
final allowBackgroundPlayback =
Copy link
Member

Choose a reason for hiding this comment

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

nit: The linter isn't flagging this for omitting the datatype?

Copy link
Contributor

Choose a reason for hiding this comment

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

We haven't converted video_player off of the legacy options yet unfortunately.

videoPlayerOptions?.allowBackgroundPlayback ?? false;
if (!allowBackgroundPlayback) {
_lifeCycleObserver = _VideoAppLifeCycleObserver(this);
}
_lifeCycleObserver?.initialize();
_creatingCompleter = Completer<void>();

late DataSource dataSourceDescription;
Expand Down Expand Up @@ -415,7 +419,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
await _eventSubscription?.cancel();
await _videoPlayerPlatform.dispose(_textureId);
}
_lifeCycleObserver.dispose();
_lifeCycleObserver?.dispose();
}
_isDisposed = true;
super.dispose();
Expand Down
12 changes: 10 additions & 2 deletions packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ 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.2.13

version: 2.3.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -23,10 +24,17 @@ flutter:
dependencies:
flutter:
sdk: flutter
video_player_platform_interface: ">=4.2.0 <6.0.0"
video_player_platform_interface:
path:
../video_player_platform_interface
video_player_web: ^2.0.0
html: ^0.15.0

dependency_overrides:
video_player_platform_interface:
path:
../video_player_platform_interface

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
57 changes: 49 additions & 8 deletions packages/video_player/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ class _FakeClosedCaptionFile extends ClosedCaptionFile {
}

void main() {
void verifyAppLifeCycle(
Copy link
Member

Choose a reason for hiding this comment

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

nit: this name isn't very helpful. Something like verifyPlayingWhenAppLifeCyclePaused?

VideoPlayerController controller, {
required bool isObserving,
}) {
final wasPlayingBeforePause = controller.value.isPlaying;
WidgetsBinding.instance!
.handleAppLifecycleStateChanged(AppLifecycleState.paused);
expect(
controller.value.isPlaying,
isObserving ? false : wasPlayingBeforePause,
);
WidgetsBinding.instance!
.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
expect(controller.value.isPlaying, wasPlayingBeforePause);
}

testWidgets('update texture', (WidgetTester tester) async {
final FakeController controller = FakeController();
await tester.pumpWidget(VideoPlayer(controller));
Expand Down Expand Up @@ -189,6 +205,15 @@ void main() {
});

group('initialize', () {
test('started app lifecycle observing', () async {
final VideoPlayerController controller = VideoPlayerController.network(
'https://127.0.0.1',
);
await controller.initialize();
await controller.play();
verifyAppLifeCycle(controller, isObserving: true);
});

test('asset', () async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: blank line between tests please.

final VideoPlayerController controller = VideoPlayerController.asset(
'a.avi',
Expand Down Expand Up @@ -810,6 +835,30 @@ void main() {
});
});

group('VideoPlayerOptions', () {
test('setMixWithOthers', () async {
final VideoPlayerController controller = VideoPlayerController.file(
File(''),
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true));
await controller.initialize();
expect(controller.videoPlayerOptions!.mixWithOthers, true);
});

[true, false].forEach((allowBackgroundPlayback) {
test('allowBackgroundPlayback is $allowBackgroundPlayback', () async {
final VideoPlayerController controller = VideoPlayerController.file(
File(''),
videoPlayerOptions: VideoPlayerOptions(
allowBackgroundPlayback: allowBackgroundPlayback,
),
);
await controller.initialize();
await controller.play();
verifyAppLifeCycle(controller, isObserving: !allowBackgroundPlayback);
});
});
});

test('VideoProgressColors', () {
const Color playedColor = Color.fromRGBO(0, 0, 255, 0.75);
const Color bufferedColor = Color.fromRGBO(0, 255, 0, 0.5);
Expand All @@ -824,14 +873,6 @@ void main() {
expect(colors.bufferedColor, bufferedColor);
expect(colors.backgroundColor, backgroundColor);
});

test('setMixWithOthers', () async {
final VideoPlayerController controller = VideoPlayerController.file(
File(''),
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true));
await controller.initialize();
expect(controller.videoPlayerOptions!.mixWithOthers, true);
});
}

class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.1.0

* Adds `allowBackgroundPlayback` to `VideoPlayerOptions`.

## 5.0.1

* Update to use the `verify` method introduced in platform_plugin_interface 2.1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ class DurationRange {

/// [VideoPlayerOptions] can be optionally used to set additional player settings
class VideoPlayerOptions {
/// Set this to true to keep playing video in background, when app goes in background.
/// The default value is false.
final bool allowBackgroundPlayback;

/// Set this to true to mix the video players audio with other audio sources.
/// The default value is false
///
Expand All @@ -337,5 +341,8 @@ class VideoPlayerOptions {
final bool mixWithOthers;

/// set additional optional player settings
VideoPlayerOptions({this.mixWithOthers = false});
VideoPlayerOptions({
this.mixWithOthers = false,
this.allowBackgroundPlayback = false,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/video_player
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
# 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: 5.0.1
version: 5.1.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down