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

[video_player] Add optional web options #6715

Closed
wants to merge 2 commits into from
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
12 changes: 11 additions & 1 deletion packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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

if (videoPlayerOptions?.webOptions != null) {
await _videoPlayerPlatform.setWebOptions(
_textureId, videoPlayerOptions!.webOptions!);
}

void eventListener(VideoEvent event) {
if (_isDisposed) {
return;
Expand Down
13 changes: 9 additions & 4 deletions packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion packages/video_player/video_player_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ abstract class VideoPlayerPlatform extends PlatformInterface {
Future<void> setMixWithOthers(bool mixWithOthers) {
throw UnimplementedError('setMixWithOthers() has not been implemented.');
}

/// Sets additional options on web
Future<void> setWebOptions(int textureId, VideoPlayerWebOptions controls) {
throw UnimplementedError('setWebOptions() has not been implemented.');
}
}

class _PlaceholderImplementation extends VideoPlayerPlatform {}
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: ../

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class VideoPlayer {

final StreamController<VideoEvent> _eventController;
final html.VideoElement _videoElement;
dynamic Function(html.Event)? _onContextMenu;

bool _isInitialized = false;
bool _isBuffering = false;
Expand Down Expand Up @@ -188,9 +189,40 @@ class VideoPlayer {
return Duration(milliseconds: (_videoElement.currentTime * 1000).round());
}

/// Sets control options
Future<void> setControls(VideoPlayerWebOptions controls) async {
if (controls.controlsEnabled) {
_videoElement.controls = true;
final List<String> attributes = <String>[];
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class VideoPlayerPlugin extends VideoPlayerPlatform {
return _player(textureId).events;
}

@override
Future<void> 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) {
Expand Down
4 changes: 3 additions & 1 deletion packages/video_player/video_player_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down