From 9aa5d0bbb810edb30777e4ac2f129a36d844c8b5 Mon Sep 17 00:00:00 2001 From: Achraf Labidi Date: Sat, 3 Feb 2024 21:29:57 +0100 Subject: [PATCH] optimise videoPlayer to cache `PlaybackSpeed` --- lib/providers.dart | 5 +++++ .../playback_speed_settings_view.dart | 18 ++++++++++++++---- .../video_view/utils/video_player_handler.dart | 1 + lib/views/video_view/video_player.dart | 7 +++---- .../video_view/video_player_controller.dart | 17 +---------------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/providers.dart b/lib/providers.dart index 909ba3d..923303b 100644 --- a/lib/providers.dart +++ b/lib/providers.dart @@ -81,3 +81,8 @@ final progressProvider = FutureProvider.autoDispose.family( final isSearchActiveProvider = StateProvider((ref) => false); + +final playbackSpeedsProvider = StateProvider>((ref) { + return [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]; +}); + diff --git a/lib/views/settings_view/playback_speed_settings_view.dart b/lib/views/settings_view/playback_speed_settings_view.dart index 5a96a4a..40796f9 100644 --- a/lib/views/settings_view/playback_speed_settings_view.dart +++ b/lib/views/settings_view/playback_speed_settings_view.dart @@ -13,8 +13,15 @@ class PlaybackSpeedSettings extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final settingState = ref.watch(settingViewModelProvider); - final selectedPlaybackSpeeds = - _parsePlaybackSpeeds(settingState.userSettings, ref); + + WidgetsBinding.instance.addPostFrameCallback((_) { + final speeds = _initPlaybackSpeeds(settingState.userSettings, ref); + // Only update if necessary to avoid infinite loops + if (ref.read(playbackSpeedsProvider.notifier).state != speeds) { + ref.read(playbackSpeedsProvider.notifier).state = speeds; + } + }); + final selectedPlaybackSpeeds = ref.watch(playbackSpeedsProvider); return Column( children: [ @@ -68,10 +75,13 @@ class PlaybackSpeedSettings extends ConsumerWidget { .updateSelectedSpeeds(speed, isSelected); } - List _parsePlaybackSpeeds( + List _initPlaybackSpeeds( List? userSettings, WidgetRef ref, ) { - return ref.read(settingViewModelProvider.notifier).parsePlaybackSpeeds(); + final settingViewModel = ref.read(settingViewModelProvider.notifier); + final speeds = settingViewModel.parsePlaybackSpeeds(); + ref.read(playbackSpeedsProvider.notifier).state = speeds; + return ref.read(playbackSpeedsProvider); } } diff --git a/lib/views/video_view/utils/video_player_handler.dart b/lib/views/video_view/utils/video_player_handler.dart index 1d63e42..5842d6e 100644 --- a/lib/views/video_view/utils/video_player_handler.dart +++ b/lib/views/video_view/utils/video_player_handler.dart @@ -38,4 +38,5 @@ class VideoPlayerHandlers { void handleToggleChat() { onToggleChat(); } + } diff --git a/lib/views/video_view/video_player.dart b/lib/views/video_view/video_player.dart index 7e6f1c9..18b17ab 100644 --- a/lib/views/video_view/video_player.dart +++ b/lib/views/video_view/video_player.dart @@ -176,19 +176,18 @@ class VideoPlayerPageState extends ConsumerState { void _setupProgressListener() { _progressTimer = - Timer.periodic(const Duration(seconds: 5), _progressUpdateCallback); + Timer.periodic(const Duration(seconds: 15), _progressUpdateCallback); } void _progressUpdateCallback(Timer timer) async { if (!_isPlayerInitialized()) return; - if (_controllerManager.videoPlayerController.value.isPlaying) { + if(!_controllerManager.videoPlayerController.value.isPlaying) return; final position = _getCurrentPosition(); final progress = _calculateProgress(position); await _updateProgress(progress); if (_shouldMarkAsWatched(progress)) { await _markStreamAsWatched(); } - } } bool _isPlayerInitialized() { @@ -212,7 +211,7 @@ class VideoPlayerPageState extends ConsumerState { } bool _shouldMarkAsWatched(double progress) { - const watchedThreshold = 0.9; + const watchedThreshold = 0.8; return progress >= watchedThreshold; } diff --git a/lib/views/video_view/video_player_controller.dart b/lib/views/video_view/video_player_controller.dart index da5a54d..e6856d8 100644 --- a/lib/views/video_view/video_player_controller.dart +++ b/lib/views/video_view/video_player_controller.dart @@ -47,7 +47,6 @@ class VideoPlayerControllerManager { aspectRatio: videoPlayerController.value.aspectRatio, autoPlay: true, looping: false, - zoomAndPan: true, additionalOptions: (context) => _getAdditionalOptions(), errorBuilder: (context, errorMessage) { return Center( @@ -62,17 +61,12 @@ class VideoPlayerControllerManager { ), ); }, - isLive: currentStream.liveNow, allowMuting: true, - useRootNavigator: true, cupertinoProgressColors: _getCupertinoProgressColors(), materialProgressColors: _getMaterialProgressColors(), placeholder: Container(color: Colors.black), - autoInitialize: true, allowFullScreen: true, - fullScreenByDefault: false, playbackSpeeds: _filteredPlaybackSpeeds(), - allowPlaybackSpeedChanging: true, ); } @@ -166,16 +160,7 @@ class VideoPlayerControllerManager { } } - List _getPlaybackSpeeds() { - final settingViewModel = ref.read(settingViewModelProvider.notifier); - return settingViewModel.parsePlaybackSpeeds(); - } - List _filteredPlaybackSpeeds() { - final playbackSpeeds = _getPlaybackSpeeds(); - var filteredSpeeds = playbackSpeeds.where((speed) => speed <= 2.0).toList(); - return filteredSpeeds.isEmpty - ? [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2] - : filteredSpeeds; + return ref.read(playbackSpeedsProvider); } }