Skip to content

Commit 6b024ca

Browse files
ditmandefuncart
andauthored
[video_player] Exposes VideoPlayerWebOptions. (#6990)
Exports types `VideoPlayerWebOptions` and `VideoPlayerWebOptionsControls` to customize the `webOptions` field in `VideoPlayerOptions` objects. Forwards `webOptions` to the web implementation. ## Usage Can be used as follows: ```dart _controller = VideoPlayerController.asset( 'assets/Butterfly-209.mp4', videoPlayerOptions: const VideoPlayerOptions( webOptions: VideoPlayerWebOptions( controls: VideoPlayerWebOptionsControls.enabled( allowDownload: false, allowFullscreen: false, allowPlaybackRate: false, allowPictureInPicture: false, ), allowContextMenu: false, allowRemotePlayback: false, ), ), ); ``` <img width="967" alt="Bildschirmfoto 2023-07-06 um 17 04 59" src="https://github.com/flutter/packages/assets/13286425/0fa92713-11cb-4073-86cf-2ea4ba486a6c"> ## Issues * Fixes: flutter/flutter#150327 * Fixes: flutter/flutter#64397 * Fixes: flutter/flutter#62192 * Fixes part of: flutter/flutter#88151 * Lands: #3259 (adds missing test) Co-authored-by: James Leahy <[email protected]>
1 parent a9240e6 commit 6b024ca

File tree

5 files changed

+76
-10
lines changed

5 files changed

+76
-10
lines changed

packages/video_player/video_player/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.9.0
2+
3+
* Exports types: `VideoPlayerWebOptions` and `VideoPlayerWebOptionsControls` to
4+
customize the `webOptions` field in `VideoPlayerOptions` objects.
5+
* Forwards `webOptions` to the web implementation.
6+
17
## 2.8.7
28

39
* Ensures that `value.position` never reports a value larger than `value.duration`.

packages/video_player/video_player/lib/video_player.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import 'package:video_player_platform_interface/video_player_platform_interface.
1414
import 'src/closed_caption_file.dart';
1515

1616
export 'package:video_player_platform_interface/video_player_platform_interface.dart'
17-
show DataSourceType, DurationRange, VideoFormat, VideoPlayerOptions;
17+
show
18+
DataSourceType,
19+
DurationRange,
20+
VideoFormat,
21+
VideoPlayerOptions,
22+
VideoPlayerWebOptions,
23+
VideoPlayerWebOptionsControls;
1824

1925
export 'src/closed_caption_file.dart';
2026

@@ -436,6 +442,14 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
436442
_creatingCompleter!.complete(null);
437443
final Completer<void> initializingCompleter = Completer<void>();
438444

445+
// Apply the web-specific options
446+
if (kIsWeb && videoPlayerOptions?.webOptions != null) {
447+
await _videoPlayerPlatform.setWebOptions(
448+
_textureId,
449+
videoPlayerOptions!.webOptions!,
450+
);
451+
}
452+
439453
void eventListener(VideoEvent event) {
440454
if (_isDisposed) {
441455
return;

packages/video_player/video_player/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for displaying inline video with other Flutter
33
widgets on Android, iOS, and web.
44
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
6-
version: 2.8.7
6+
version: 2.9.0
77

88
environment:
99
sdk: ">=3.2.3 <4.0.0"
@@ -27,7 +27,7 @@ dependencies:
2727
html: ^0.15.0
2828
video_player_android: ^2.3.5
2929
video_player_avfoundation: ^2.5.6
30-
video_player_platform_interface: ">=6.1.0 <7.0.0"
30+
video_player_platform_interface: ^6.2.0
3131
video_player_web: ^2.0.0
3232

3333
dev_dependencies:

packages/video_player/video_player/test/video_player_initialization_test.dart

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,59 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter_test/flutter_test.dart';
67
import 'package:video_player/video_player.dart';
78
import 'package:video_player_platform_interface/video_player_platform_interface.dart';
89

910
import 'video_player_test.dart' show FakeVideoPlayerPlatform;
1011

1112
void main() {
12-
// This test needs to run first and therefore needs to be the only test
13-
// in this file.
14-
test('plugin initialized', () async {
15-
TestWidgetsFlutterBinding.ensureInitialized();
16-
final FakeVideoPlayerPlatform fakeVideoPlayerPlatform =
17-
FakeVideoPlayerPlatform();
18-
VideoPlayerPlatform.instance = fakeVideoPlayerPlatform;
13+
TestWidgetsFlutterBinding.ensureInitialized();
14+
15+
late FakeVideoPlayerPlatform fakeVideoPlayerPlatform;
16+
17+
setUp(() {
18+
VideoPlayerPlatform.instance =
19+
fakeVideoPlayerPlatform = FakeVideoPlayerPlatform();
20+
});
1921

22+
test('plugin initialized', () async {
2023
final VideoPlayerController controller = VideoPlayerController.networkUrl(
2124
Uri.parse('https://127.0.0.1'),
2225
);
2326
await controller.initialize();
2427
expect(fakeVideoPlayerPlatform.calls.first, 'init');
2528
});
29+
30+
test('web configuration is applied (web only)', () async {
31+
const VideoPlayerWebOptions expected = VideoPlayerWebOptions(
32+
allowContextMenu: false,
33+
allowRemotePlayback: false,
34+
controls: VideoPlayerWebOptionsControls.enabled(),
35+
);
36+
37+
final VideoPlayerController controller = VideoPlayerController.networkUrl(
38+
Uri.parse('https://127.0.0.1'),
39+
videoPlayerOptions: VideoPlayerOptions(
40+
webOptions: expected,
41+
),
42+
);
43+
await controller.initialize();
44+
45+
expect(
46+
() {
47+
fakeVideoPlayerPlatform.calls.singleWhere(
48+
(String call) => call == 'setWebOptions',
49+
);
50+
},
51+
returnsNormally,
52+
reason: 'setWebOptions must be called exactly once.',
53+
);
54+
expect(
55+
fakeVideoPlayerPlatform.webOptions[controller.textureId],
56+
expected,
57+
reason: 'web options must be passed to the platform',
58+
);
59+
}, skip: !kIsWeb);
2660
}

packages/video_player/video_player/test/video_player_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,8 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
13111311
bool forceInitError = false;
13121312
int nextTextureId = 0;
13131313
final Map<int, Duration> _positions = <int, Duration>{};
1314+
final Map<int, VideoPlayerWebOptions> webOptions =
1315+
<int, VideoPlayerWebOptions>{};
13141316

13151317
@override
13161318
Future<int?> create(DataSource dataSource) async {
@@ -1392,4 +1394,14 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
13921394
Widget buildView(int textureId) {
13931395
return Texture(textureId: textureId);
13941396
}
1397+
1398+
@override
1399+
Future<void> setWebOptions(
1400+
int textureId, VideoPlayerWebOptions options) async {
1401+
if (!kIsWeb) {
1402+
throw UnimplementedError('setWebOptions() is only available in the web.');
1403+
}
1404+
calls.add('setWebOptions');
1405+
webOptions[textureId] = options;
1406+
}
13951407
}

0 commit comments

Comments
 (0)