Skip to content

Commit

Permalink
optimise videoPlayer to cache PlaybackSpeed
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Feb 3, 2024
1 parent d60d41a commit 9aa5d0b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
5 changes: 5 additions & 0 deletions lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ final progressProvider = FutureProvider.autoDispose.family<Progress, int>(

final isSearchActiveProvider = StateProvider<bool>((ref) => false);


final playbackSpeedsProvider = StateProvider<List<double>>((ref) {
return [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2];
});

18 changes: 14 additions & 4 deletions lib/views/settings_view/playback_speed_settings_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -68,10 +75,13 @@ class PlaybackSpeedSettings extends ConsumerWidget {
.updateSelectedSpeeds(speed, isSelected);
}

List<double> _parsePlaybackSpeeds(
List<double> _initPlaybackSpeeds(
List<UserSetting>? 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);
}
}
1 change: 1 addition & 0 deletions lib/views/video_view/utils/video_player_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ class VideoPlayerHandlers {
void handleToggleChat() {
onToggleChat();
}

}
7 changes: 3 additions & 4 deletions lib/views/video_view/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,18 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {

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() {
Expand All @@ -212,7 +211,7 @@ class VideoPlayerPageState extends ConsumerState<VideoPlayerPage> {
}

bool _shouldMarkAsWatched(double progress) {
const watchedThreshold = 0.9;
const watchedThreshold = 0.8;
return progress >= watchedThreshold;
}

Expand Down
17 changes: 1 addition & 16 deletions lib/views/video_view/video_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class VideoPlayerControllerManager {
aspectRatio: videoPlayerController.value.aspectRatio,
autoPlay: true,
looping: false,
zoomAndPan: true,
additionalOptions: (context) => _getAdditionalOptions(),
errorBuilder: (context, errorMessage) {
return Center(
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -166,16 +160,7 @@ class VideoPlayerControllerManager {
}
}

List<double> _getPlaybackSpeeds() {
final settingViewModel = ref.read(settingViewModelProvider.notifier);
return settingViewModel.parsePlaybackSpeeds();
}

List<double> _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);
}
}

0 comments on commit 9aa5d0b

Please sign in to comment.