From 480de69ab2ec303699dce6ea1715871b34c0cf6b Mon Sep 17 00:00:00 2001 From: James Leahy Date: Fri, 18 Nov 2022 07:37:35 +0100 Subject: [PATCH 1/2] feat: web add control options --- .../video_player/lib/video_player.dart | 12 +++++- .../lib/video_player_platform_interface.dart | 43 +++++++++++++++++++ .../lib/src/video_player.dart | 32 ++++++++++++++ .../lib/video_player_web.dart | 5 +++ 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/packages/video_player/video_player/lib/video_player.dart b/packages/video_player/video_player/lib/video_player.dart index 3dbdcb543082..9dda8df751a7 100644 --- a/packages/video_player/video_player/lib/video_player.dart +++ b/packages/video_player/video_player/lib/video_player.dart @@ -14,7 +14,12 @@ import 'package:video_player_platform_interface/video_player_platform_interface. import 'src/closed_caption_file.dart'; export 'package:video_player_platform_interface/video_player_platform_interface.dart' - show DurationRange, DataSourceType, VideoFormat, VideoPlayerOptions; + show + DurationRange, + DataSourceType, + VideoFormat, + VideoPlayerOptions, + VideoPlayerWebOptions; export 'src/closed_caption_file.dart'; @@ -364,6 +369,11 @@ class VideoPlayerController extends ValueNotifier { _creatingCompleter!.complete(null); final Completer initializingCompleter = Completer(); + if (videoPlayerOptions?.webOptions != null) { + await _videoPlayerPlatform.setWebOptions( + _textureId, videoPlayerOptions!.webOptions!); + } + void eventListener(VideoEvent event) { if (_isDisposed) { return; diff --git a/packages/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart b/packages/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart index d3df9b25df53..2ef146e8f1d7 100644 --- a/packages/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart +++ b/packages/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart @@ -102,6 +102,11 @@ abstract class VideoPlayerPlatform extends PlatformInterface { Future setMixWithOthers(bool mixWithOthers) { throw UnimplementedError('setMixWithOthers() has not been implemented.'); } + + /// Sets additional options on web + Future setWebOptions(int textureId, VideoPlayerWebOptions controls) { + throw UnimplementedError('setWebOptions() has not been implemented.'); + } } class _PlaceholderImplementation extends VideoPlayerPlatform {} @@ -359,6 +364,7 @@ class VideoPlayerOptions { VideoPlayerOptions({ this.mixWithOthers = false, this.allowBackgroundPlayback = false, + this.webOptions, }); /// Set this to true to keep playing video in background, when app goes in background. @@ -371,4 +377,41 @@ class VideoPlayerOptions { /// Note: This option will be silently ignored in the web platform (there is /// currently no way to implement this feature in this platform). final bool mixWithOthers; + + /// Additional web controls + final VideoPlayerWebOptions? webOptions; +} + +/// [VideoPlayerWebOptions] can be optionally used to set additional web settings +@immutable +class VideoPlayerWebOptions { + /// [VideoPlayerWebOptions] can be optionally used to set additional web settings + const VideoPlayerWebOptions({ + this.controlsEnabled = false, + this.allowDownload = true, + this.allowFullscreen = true, + this.allowPlaybackRate = true, + this.allowContextMenu = true, + }); + + /// Whether native controls are enabled + final bool controlsEnabled; + + /// Whether downloaded control is displayed + /// + /// Only applicable when [controlsEnabled] is true + final bool allowDownload; + + /// Whether fullscreen control is enabled + /// + /// Only applicable when [controlsEnabled] is true + final bool allowFullscreen; + + /// Whether playback rate control is displayed + /// + /// Only applicable when [controlsEnabled] is true + final bool allowPlaybackRate; + + /// Whether context menu (right click) is allowed + final bool allowContextMenu; } diff --git a/packages/video_player/video_player_web/lib/src/video_player.dart b/packages/video_player/video_player_web/lib/src/video_player.dart index 02ead1fdf93b..dd43ffc942a4 100644 --- a/packages/video_player/video_player_web/lib/src/video_player.dart +++ b/packages/video_player/video_player_web/lib/src/video_player.dart @@ -45,6 +45,7 @@ class VideoPlayer { final StreamController _eventController; final html.VideoElement _videoElement; + dynamic Function(html.Event)? _onContextMenu; bool _isInitialized = false; bool _isBuffering = false; @@ -188,9 +189,40 @@ class VideoPlayer { return Duration(milliseconds: (_videoElement.currentTime * 1000).round()); } + /// Sets control options + Future setControls(VideoPlayerWebOptions controls) async { + if (controls.controlsEnabled) { + _videoElement.controls = true; + final List attributes = []; + if (!controls.allowDownload) { + attributes.add('nodownload'); + } + if (!controls.allowFullscreen) { + attributes.add('nofullscreen'); + } + if (!controls.allowPlaybackRate) { + attributes.add('noplaybackrate'); + } + if (attributes.isNotEmpty) { + _videoElement.setAttribute( + 'controlsList', + attributes + .reduce((String value, String element) => '$value $element'), + ); + } + } + + if (!controls.allowContextMenu) { + _onContextMenu = (html.Event event) => event.preventDefault(); + _videoElement.addEventListener('contextmenu', _onContextMenu); + } + } + /// Disposes of the current [html.VideoElement]. void dispose() { _videoElement.removeAttribute('src'); + _videoElement.removeEventListener('contextmenu', _onContextMenu); + _onContextMenu = null; _videoElement.load(); } diff --git a/packages/video_player/video_player_web/lib/video_player_web.dart b/packages/video_player/video_player_web/lib/video_player_web.dart index e52fd83de79e..c803fa07403a 100644 --- a/packages/video_player/video_player_web/lib/video_player_web.dart +++ b/packages/video_player/video_player_web/lib/video_player_web.dart @@ -132,6 +132,11 @@ class VideoPlayerPlugin extends VideoPlayerPlatform { return _player(textureId).events; } + @override + Future setWebOptions(int textureId, VideoPlayerWebOptions controls) { + return _player(textureId).setControls(controls); + } + // Retrieves a [VideoPlayer] by its internal `id`. // It must have been created earlier from the [create] method. VideoPlayer _player(int id) { From b6877168b24953d1ff99866e71daefa419611112 Mon Sep 17 00:00:00 2001 From: James Leahy Date: Thu, 1 Dec 2022 16:45:11 +0100 Subject: [PATCH 2/2] ci: use paths --- packages/video_player/video_player/pubspec.yaml | 13 +++++++++---- .../video_player_android/example/pubspec.yaml | 3 ++- .../video_player/video_player_android/pubspec.yaml | 4 +++- .../video_player_avfoundation/example/pubspec.yaml | 3 ++- .../video_player_avfoundation/pubspec.yaml | 4 +++- .../video_player_web/example/pubspec.yaml | 3 ++- packages/video_player/video_player_web/pubspec.yaml | 4 +++- 7 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/video_player/video_player/pubspec.yaml b/packages/video_player/video_player/pubspec.yaml index d75456ace469..efc4f8fe52fa 100644 --- a/packages/video_player/video_player/pubspec.yaml +++ b/packages/video_player/video_player/pubspec.yaml @@ -4,6 +4,7 @@ description: Flutter plugin for displaying inline video with other Flutter repository: https://github.com/flutter/plugins/tree/main/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.5.1 +publish_to: 'none' environment: sdk: ">=2.14.0 <3.0.0" @@ -23,10 +24,14 @@ dependencies: flutter: sdk: flutter html: ^0.15.0 - video_player_android: ^2.3.5 - video_player_avfoundation: ^2.2.17 - video_player_platform_interface: ">=5.1.1 <7.0.0" - video_player_web: ^2.0.0 + video_player_android: + path: ../video_player_android + video_player_avfoundation: + path: ../video_player_avfoundation + video_player_platform_interface: + path: ../video_player_platform_interface + video_player_web: + path: ../video_player_web dev_dependencies: flutter_test: diff --git a/packages/video_player/video_player_android/example/pubspec.yaml b/packages/video_player/video_player_android/example/pubspec.yaml index 16ffe17e7ba3..3eff590e163c 100644 --- a/packages/video_player/video_player_android/example/pubspec.yaml +++ b/packages/video_player/video_player_android/example/pubspec.yaml @@ -16,7 +16,8 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - video_player_platform_interface: ">=5.1.1 <7.0.0" + video_player_platform_interface: + path: ../../video_player_platform_interface dev_dependencies: flutter_driver: diff --git a/packages/video_player/video_player_android/pubspec.yaml b/packages/video_player/video_player_android/pubspec.yaml index 3f46ec8a4d79..4461ba1b42ce 100644 --- a/packages/video_player/video_player_android/pubspec.yaml +++ b/packages/video_player/video_player_android/pubspec.yaml @@ -3,6 +3,7 @@ description: Android implementation of the video_player plugin. repository: https://github.com/flutter/plugins/tree/main/packages/video_player/video_player_android issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22 version: 2.3.10 +publish_to: 'none' environment: sdk: ">=2.14.0 <3.0.0" @@ -20,7 +21,8 @@ flutter: dependencies: flutter: sdk: flutter - video_player_platform_interface: ">=5.1.1 <7.0.0" + video_player_platform_interface: + path: ../video_player_platform_interface dev_dependencies: flutter_test: diff --git a/packages/video_player/video_player_avfoundation/example/pubspec.yaml b/packages/video_player/video_player_avfoundation/example/pubspec.yaml index 422fb91e35e5..19650a5ac44b 100644 --- a/packages/video_player/video_player_avfoundation/example/pubspec.yaml +++ b/packages/video_player/video_player_avfoundation/example/pubspec.yaml @@ -16,7 +16,8 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - video_player_platform_interface: ">=4.2.0 <7.0.0" + video_player_platform_interface: + path: ../../video_player_platform_interface dev_dependencies: flutter_driver: diff --git a/packages/video_player/video_player_avfoundation/pubspec.yaml b/packages/video_player/video_player_avfoundation/pubspec.yaml index a5204137af20..fb8e1ea84af7 100644 --- a/packages/video_player/video_player_avfoundation/pubspec.yaml +++ b/packages/video_player/video_player_avfoundation/pubspec.yaml @@ -3,6 +3,7 @@ description: iOS implementation of the video_player plugin. repository: https://github.com/flutter/plugins/tree/main/packages/video_player/video_player_avfoundation issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22 version: 2.3.8 +publish_to: 'none' environment: sdk: ">=2.14.0 <3.0.0" @@ -19,7 +20,8 @@ flutter: dependencies: flutter: sdk: flutter - video_player_platform_interface: ">=4.2.0 <7.0.0" + video_player_platform_interface: + path: ../video_player_platform_interface dev_dependencies: flutter_test: diff --git a/packages/video_player/video_player_web/example/pubspec.yaml b/packages/video_player/video_player_web/example/pubspec.yaml index c4de1ce54c1a..bbc319705eb3 100644 --- a/packages/video_player/video_player_web/example/pubspec.yaml +++ b/packages/video_player/video_player_web/example/pubspec.yaml @@ -9,7 +9,8 @@ dependencies: flutter: sdk: flutter js: ^0.6.0 - video_player_platform_interface: ">=4.2.0 <7.0.0" + video_player_platform_interface: + path: ../../video_player_platform_interface video_player_web: path: ../ diff --git a/packages/video_player/video_player_web/pubspec.yaml b/packages/video_player/video_player_web/pubspec.yaml index 5e603034dd28..6402a4b7e98f 100644 --- a/packages/video_player/video_player_web/pubspec.yaml +++ b/packages/video_player/video_player_web/pubspec.yaml @@ -3,6 +3,7 @@ description: Web platform implementation of video_player. repository: https://github.com/flutter/plugins/tree/main/packages/video_player/video_player_web issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22 version: 2.0.13 +publish_to: 'none' environment: sdk: ">=2.12.0 <3.0.0" @@ -21,7 +22,8 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter - video_player_platform_interface: ">=4.2.0 <7.0.0" + video_player_platform_interface: + path: ../video_player_platform_interface dev_dependencies: flutter_test: