Skip to content

Commit be35a18

Browse files
authored
[video_player] Add html 5 video poster support (thumbnail) as a VideoPlayerWebOptions (#8940)
This PR adds support for the `poster` attribute on web by introducing a `poster` field in `VideoPlayerWebOptions`. It allows setting a thumbnail image for videos using the native HTML5 `poster` property, improving the out-of-the-box web experience. Fixes flutter/flutter#166232 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 4a231ae commit be35a18

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

packages/video_player/video_player_web/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.4.0
22

3+
* Adds HTML5 video poster support as a VideoPlayerWebOptions.
34
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
45

56
## 2.3.5

packages/video_player/video_player_web/example/integration_test/video_player_test.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,78 @@ void main() {
434434
});
435435
});
436436

437+
group('poster', () {
438+
testWidgets('when null expect no poster attribute',
439+
(WidgetTester tester) async {
440+
await player.setOptions(
441+
const VideoPlayerWebOptions(),
442+
);
443+
444+
expect(video.poster, isEmpty);
445+
expect(video.getAttribute('poster'), isNull);
446+
});
447+
448+
testWidgets('when provided expect poster attribute set',
449+
(WidgetTester tester) async {
450+
final Uri posterUri = Uri.parse('https://example.com/poster.jpg');
451+
await player.setOptions(
452+
VideoPlayerWebOptions(
453+
poster: posterUri,
454+
),
455+
);
456+
457+
expect(video.poster, posterUri.toString());
458+
expect(video.getAttribute('poster'), posterUri.toString());
459+
});
460+
461+
testWidgets('when set to null after having value expect poster removed',
462+
(WidgetTester tester) async {
463+
final Uri posterUri = Uri.parse('https://example.com/poster.jpg');
464+
465+
await player.setOptions(
466+
VideoPlayerWebOptions(
467+
poster: posterUri,
468+
),
469+
);
470+
471+
expect(video.poster, posterUri.toString());
472+
473+
await player.setOptions(
474+
const VideoPlayerWebOptions(),
475+
);
476+
477+
expect(video.poster, isEmpty);
478+
expect(video.getAttribute('poster'), isNull);
479+
});
480+
481+
testWidgets('when updated expect poster attribute updated',
482+
(WidgetTester tester) async {
483+
final Uri initialPoster =
484+
Uri.parse('https://example.com/poster1.jpg');
485+
final Uri updatedPoster =
486+
Uri.parse('https://example.com/poster2.jpg');
487+
488+
// Set initial poster
489+
await player.setOptions(
490+
VideoPlayerWebOptions(
491+
poster: initialPoster,
492+
),
493+
);
494+
495+
expect(video.poster, initialPoster.toString());
496+
497+
// Update poster
498+
await player.setOptions(
499+
VideoPlayerWebOptions(
500+
poster: updatedPoster,
501+
),
502+
);
503+
504+
expect(video.poster, updatedPoster.toString());
505+
expect(video.getAttribute('poster'), updatedPoster.toString());
506+
});
507+
});
508+
437509
group('when called first time', () {
438510
testWidgets('expect correct options', (WidgetTester tester) async {
439511
await player.setOptions(

packages/video_player/video_player_web/lib/src/video_player.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ class VideoPlayer {
262262
if (!options.allowRemotePlayback) {
263263
_videoElement.disableRemotePlayback = true;
264264
}
265+
266+
if (options.poster != null) {
267+
_videoElement.poster = options.poster!.toString();
268+
}
265269
}
266270

267271
void _resetOptions() {
@@ -273,6 +277,7 @@ class VideoPlayer {
273277
_onContextMenu = null;
274278
}
275279
_videoElement.removeAttribute('disableRemotePlayback');
280+
_videoElement.removeAttribute('poster');
276281
}
277282

278283
/// Disposes of the current [web.HTMLVideoElement].

packages/video_player/video_player_web/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: video_player_web
22
description: Web platform implementation of video_player.
33
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
5-
version: 2.3.5
5+
version: 2.4.0
66

77
environment:
88
sdk: ^3.6.0
@@ -21,7 +21,7 @@ dependencies:
2121
sdk: flutter
2222
flutter_web_plugins:
2323
sdk: flutter
24-
video_player_platform_interface: ^6.3.0
24+
video_player_platform_interface: ^6.4.0
2525
web: ">=0.5.1 <2.0.0"
2626

2727
dev_dependencies:

0 commit comments

Comments
 (0)