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

Commit 9d6abf8

Browse files
committed
feat: web add control options
1 parent b751ff1 commit 9d6abf8

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

packages/video_player/video_player/lib/video_player.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ 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 DurationRange, DataSourceType, VideoFormat, VideoPlayerOptions;
17+
show
18+
DurationRange,
19+
DataSourceType,
20+
VideoFormat,
21+
VideoPlayerOptions,
22+
VideoPlayerWebOptions;
1823

1924
export 'src/closed_caption_file.dart';
2025

@@ -364,6 +369,11 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
364369
_creatingCompleter!.complete(null);
365370
final Completer<void> initializingCompleter = Completer<void>();
366371

372+
if (videoPlayerOptions?.webOptions != null) {
373+
await _videoPlayerPlatform.setWebOptions(
374+
_textureId, videoPlayerOptions!.webOptions!);
375+
}
376+
367377
void eventListener(VideoEvent event) {
368378
if (_isDisposed) {
369379
return;

packages/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ abstract class VideoPlayerPlatform extends PlatformInterface {
102102
Future<void> setMixWithOthers(bool mixWithOthers) {
103103
throw UnimplementedError('setMixWithOthers() has not been implemented.');
104104
}
105+
106+
/// Sets additional options on web
107+
Future<void> setWebOptions(int textureId, VideoPlayerWebOptions controls) {
108+
throw UnimplementedError('setWebOptions() has not been implemented.');
109+
}
105110
}
106111

107112
class _PlaceholderImplementation extends VideoPlayerPlatform {}
@@ -359,6 +364,7 @@ class VideoPlayerOptions {
359364
VideoPlayerOptions({
360365
this.mixWithOthers = false,
361366
this.allowBackgroundPlayback = false,
367+
this.webOptions,
362368
});
363369

364370
/// Set this to true to keep playing video in background, when app goes in background.
@@ -371,4 +377,41 @@ class VideoPlayerOptions {
371377
/// Note: This option will be silently ignored in the web platform (there is
372378
/// currently no way to implement this feature in this platform).
373379
final bool mixWithOthers;
380+
381+
/// Additional web controls
382+
final VideoPlayerWebOptions? webOptions;
383+
}
384+
385+
/// [VideoPlayerWebOptions] can be optionally used to set additional web settings
386+
@immutable
387+
class VideoPlayerWebOptions {
388+
/// [VideoPlayerWebOptions] can be optionally used to set additional web settings
389+
const VideoPlayerWebOptions({
390+
this.controlsEnabled = false,
391+
this.allowDownload = true,
392+
this.allowFullscreen = true,
393+
this.allowPlaybackRate = true,
394+
this.allowContextMenu = true,
395+
});
396+
397+
/// Whether native controls are enabled
398+
final bool controlsEnabled;
399+
400+
/// Whether downloaded control is displayed
401+
///
402+
/// Only applicable when [controlsEnabled] is true
403+
final bool allowDownload;
404+
405+
/// Whether fullscreen control is enabled
406+
///
407+
/// Only applicable when [controlsEnabled] is true
408+
final bool allowFullscreen;
409+
410+
/// Whether playback rate control is displayed
411+
///
412+
/// Only applicable when [controlsEnabled] is true
413+
final bool allowPlaybackRate;
414+
415+
/// Whether context menu (right click) is allowed
416+
final bool allowContextMenu;
374417
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class VideoPlayer {
4545

4646
final StreamController<VideoEvent> _eventController;
4747
final html.VideoElement _videoElement;
48+
dynamic Function(html.Event)? _onContextMenu;
4849

4950
bool _isInitialized = false;
5051
bool _isBuffering = false;
@@ -188,9 +189,40 @@ class VideoPlayer {
188189
return Duration(milliseconds: (_videoElement.currentTime * 1000).round());
189190
}
190191

192+
/// Sets control options
193+
Future<void> setControls(VideoPlayerWebOptions controls) async {
194+
if (controls.controlsEnabled) {
195+
_videoElement.controls = true;
196+
final List<String> attributes = <String>[];
197+
if (!controls.allowDownload) {
198+
attributes.add('nodownload');
199+
}
200+
if (!controls.allowFullscreen) {
201+
attributes.add('nofullscreen');
202+
}
203+
if (!controls.allowPlaybackRate) {
204+
attributes.add('noplaybackrate');
205+
}
206+
if (attributes.isNotEmpty) {
207+
_videoElement.setAttribute(
208+
'controlsList',
209+
attributes
210+
.reduce((String value, String element) => '$value $element'),
211+
);
212+
}
213+
}
214+
215+
if (!controls.allowContextMenu) {
216+
_onContextMenu = (html.Event event) => event.preventDefault();
217+
_videoElement.addEventListener('contextmenu', _onContextMenu);
218+
}
219+
}
220+
191221
/// Disposes of the current [html.VideoElement].
192222
void dispose() {
193223
_videoElement.removeAttribute('src');
224+
_videoElement.removeEventListener('contextmenu', _onContextMenu);
225+
_onContextMenu = null;
194226
_videoElement.load();
195227
}
196228

packages/video_player/video_player_web/lib/video_player_web.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ class VideoPlayerPlugin extends VideoPlayerPlatform {
132132
return _player(textureId).events;
133133
}
134134

135+
@override
136+
Future<void> setWebOptions(int textureId, VideoPlayerWebOptions controls) {
137+
return _player(textureId).setControls(controls);
138+
}
139+
135140
// Retrieves a [VideoPlayer] by its internal `id`.
136141
// It must have been created earlier from the [create] method.
137142
VideoPlayer _player(int id) {

0 commit comments

Comments
 (0)