Skip to content

Commit a030f6b

Browse files
committed
feat: added method to set progress delay
1 parent c17107b commit a030f6b

File tree

3 files changed

+144
-49
lines changed

3 files changed

+144
-49
lines changed

example/lib/app/app.dart

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class _ChewieDemoState extends State<ChewieDemo> {
2323
late VideoPlayerController _videoPlayerController1;
2424
late VideoPlayerController _videoPlayerController2;
2525
ChewieController? _chewieController;
26+
int? bufferDelay;
2627

2728
@override
2829
void initState() {
@@ -111,6 +112,7 @@ class _ChewieDemoState extends State<ChewieDemo> {
111112
videoPlayerController: _videoPlayerController1,
112113
autoPlay: true,
113114
looping: true,
115+
progressIndicatorDelayMS: bufferDelay,
114116

115117
additionalOptions: (context) {
116118
return <OptionItem>[
@@ -304,9 +306,86 @@ class _ChewieDemoState extends State<ChewieDemo> {
304306
),
305307
],
306308
),
309+
ListTile(
310+
title: const Text("Delay"),
311+
subtitle: Column(
312+
children: [
313+
DelaySlider(
314+
delay: _chewieController?.progressIndicatorDelayMS,
315+
onSave: (delay) async {
316+
if (delay != null) {
317+
if (delay == 0) {
318+
bufferDelay = null;
319+
} else {
320+
bufferDelay = delay;
321+
}
322+
323+
await initializePlayer();
324+
}
325+
},
326+
),
327+
Text(
328+
_chewieController?.progressIndicatorDelayMS != null
329+
? "Progress indicator delay ${_chewieController!.progressIndicatorDelayMS.toString()} MS"
330+
: "Set me",
331+
),
332+
],
333+
),
334+
)
307335
],
308336
),
309337
),
310338
);
311339
}
312340
}
341+
342+
class DelaySlider extends StatefulWidget {
343+
const DelaySlider({Key? key, required this.delay, required this.onSave})
344+
: super(key: key);
345+
346+
final int? delay;
347+
final void Function(int?) onSave;
348+
@override
349+
State<DelaySlider> createState() => _DelaySliderState();
350+
}
351+
352+
class _DelaySliderState extends State<DelaySlider> {
353+
int? delay;
354+
bool saved = false;
355+
356+
@override
357+
void initState() {
358+
super.initState();
359+
delay = widget.delay;
360+
}
361+
362+
@override
363+
Widget build(BuildContext context) {
364+
const int max = 1000;
365+
return ListTile(
366+
title: Text(
367+
"Progress indicator delay ${delay != null ? "${delay.toString()} MS" : ""}",
368+
),
369+
subtitle: Slider(
370+
value: delay != null ? (delay! / max) : 0,
371+
onChanged: (value) async {
372+
delay = (value * max).toInt();
373+
setState(() {
374+
saved = false;
375+
});
376+
},
377+
),
378+
trailing: IconButton(
379+
icon: const Icon(Icons.save),
380+
onPressed: saved
381+
? null
382+
: () {
383+
widget.onSave(delay);
384+
setState(() {
385+
saved = true;
386+
});
387+
},
388+
),
389+
);
390+
}
391+
}

lib/src/chewie_player.dart

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class ChewieController extends ChangeNotifier {
280280
this.systemOverlaysAfterFullScreen = SystemUiOverlay.values,
281281
this.deviceOrientationsAfterFullScreen = DeviceOrientation.values,
282282
this.routePageBuilder,
283+
this.progressIndicatorDelayMS,
283284
}) : assert(
284285
playbackSpeeds.every((speed) => speed > 0),
285286
'The playbackSpeeds values must all be greater than 0',
@@ -322,6 +323,7 @@ class ChewieController extends ChangeNotifier {
322323
List<DeviceOrientation>? deviceOrientationsOnEnterFullScreen,
323324
List<SystemUiOverlay>? systemOverlaysAfterFullScreen,
324325
List<DeviceOrientation>? deviceOrientationsAfterFullScreen,
326+
int? bufferingProgressIndicatorDisplayDelayMS,
325327
Widget Function(
326328
BuildContext,
327329
Animation<double>,
@@ -331,50 +333,51 @@ class ChewieController extends ChangeNotifier {
331333
routePageBuilder,
332334
}) {
333335
return ChewieController(
334-
videoPlayerController:
335-
videoPlayerController ?? this.videoPlayerController,
336-
optionsTranslation: optionsTranslation ?? this.optionsTranslation,
337-
aspectRatio: aspectRatio ?? this.aspectRatio,
338-
autoInitialize: autoInitialize ?? this.autoInitialize,
339-
autoPlay: autoPlay ?? this.autoPlay,
340-
startAt: startAt ?? this.startAt,
341-
looping: looping ?? this.looping,
342-
fullScreenByDefault: fullScreenByDefault ?? this.fullScreenByDefault,
343-
cupertinoProgressColors:
344-
cupertinoProgressColors ?? this.cupertinoProgressColors,
345-
materialProgressColors:
346-
materialProgressColors ?? this.materialProgressColors,
347-
placeholder: placeholder ?? this.placeholder,
348-
overlay: overlay ?? this.overlay,
349-
showControlsOnInitialize:
350-
showControlsOnInitialize ?? this.showControlsOnInitialize,
351-
showOptions: showOptions ?? this.showOptions,
352-
optionsBuilder: optionsBuilder ?? this.optionsBuilder,
353-
additionalOptions: additionalOptions ?? this.additionalOptions,
354-
showControls: showControls ?? this.showControls,
355-
subtitle: subtitle ?? this.subtitle,
356-
subtitleBuilder: subtitleBuilder ?? this.subtitleBuilder,
357-
customControls: customControls ?? this.customControls,
358-
errorBuilder: errorBuilder ?? this.errorBuilder,
359-
allowedScreenSleep: allowedScreenSleep ?? this.allowedScreenSleep,
360-
isLive: isLive ?? this.isLive,
361-
allowFullScreen: allowFullScreen ?? this.allowFullScreen,
362-
allowMuting: allowMuting ?? this.allowMuting,
363-
allowPlaybackSpeedChanging:
364-
allowPlaybackSpeedChanging ?? this.allowPlaybackSpeedChanging,
365-
useRootNavigator: useRootNavigator ?? this.useRootNavigator,
366-
playbackSpeeds: playbackSpeeds ?? this.playbackSpeeds,
367-
systemOverlaysOnEnterFullScreen: systemOverlaysOnEnterFullScreen ??
368-
this.systemOverlaysOnEnterFullScreen,
369-
deviceOrientationsOnEnterFullScreen:
370-
deviceOrientationsOnEnterFullScreen ??
371-
this.deviceOrientationsOnEnterFullScreen,
372-
systemOverlaysAfterFullScreen:
373-
systemOverlaysAfterFullScreen ?? this.systemOverlaysAfterFullScreen,
374-
deviceOrientationsAfterFullScreen: deviceOrientationsAfterFullScreen ??
375-
this.deviceOrientationsAfterFullScreen,
376-
routePageBuilder: routePageBuilder ?? this.routePageBuilder,
377-
);
336+
videoPlayerController:
337+
videoPlayerController ?? this.videoPlayerController,
338+
optionsTranslation: optionsTranslation ?? this.optionsTranslation,
339+
aspectRatio: aspectRatio ?? this.aspectRatio,
340+
autoInitialize: autoInitialize ?? this.autoInitialize,
341+
autoPlay: autoPlay ?? this.autoPlay,
342+
startAt: startAt ?? this.startAt,
343+
looping: looping ?? this.looping,
344+
fullScreenByDefault: fullScreenByDefault ?? this.fullScreenByDefault,
345+
cupertinoProgressColors:
346+
cupertinoProgressColors ?? this.cupertinoProgressColors,
347+
materialProgressColors:
348+
materialProgressColors ?? this.materialProgressColors,
349+
placeholder: placeholder ?? this.placeholder,
350+
overlay: overlay ?? this.overlay,
351+
showControlsOnInitialize:
352+
showControlsOnInitialize ?? this.showControlsOnInitialize,
353+
showOptions: showOptions ?? this.showOptions,
354+
optionsBuilder: optionsBuilder ?? this.optionsBuilder,
355+
additionalOptions: additionalOptions ?? this.additionalOptions,
356+
showControls: showControls ?? this.showControls,
357+
subtitle: subtitle ?? this.subtitle,
358+
subtitleBuilder: subtitleBuilder ?? this.subtitleBuilder,
359+
customControls: customControls ?? this.customControls,
360+
errorBuilder: errorBuilder ?? this.errorBuilder,
361+
allowedScreenSleep: allowedScreenSleep ?? this.allowedScreenSleep,
362+
isLive: isLive ?? this.isLive,
363+
allowFullScreen: allowFullScreen ?? this.allowFullScreen,
364+
allowMuting: allowMuting ?? this.allowMuting,
365+
allowPlaybackSpeedChanging:
366+
allowPlaybackSpeedChanging ?? this.allowPlaybackSpeedChanging,
367+
useRootNavigator: useRootNavigator ?? this.useRootNavigator,
368+
playbackSpeeds: playbackSpeeds ?? this.playbackSpeeds,
369+
systemOverlaysOnEnterFullScreen: systemOverlaysOnEnterFullScreen ??
370+
this.systemOverlaysOnEnterFullScreen,
371+
deviceOrientationsOnEnterFullScreen:
372+
deviceOrientationsOnEnterFullScreen ??
373+
this.deviceOrientationsOnEnterFullScreen,
374+
systemOverlaysAfterFullScreen:
375+
systemOverlaysAfterFullScreen ?? this.systemOverlaysAfterFullScreen,
376+
deviceOrientationsAfterFullScreen: deviceOrientationsAfterFullScreen ??
377+
this.deviceOrientationsAfterFullScreen,
378+
routePageBuilder: routePageBuilder ?? this.routePageBuilder,
379+
progressIndicatorDelayMS: bufferingProgressIndicatorDisplayDelayMS ??
380+
this.progressIndicatorDelayMS);
378381
}
379382

380383
/// If false, the options button in MaterialUI and MaterialDesktopUI
@@ -505,6 +508,9 @@ class ChewieController extends ChangeNotifier {
505508
/// Defines a custom RoutePageBuilder for the fullscreen
506509
final ChewieRoutePageBuilder? routePageBuilder;
507510

511+
/// [Android] Defines a delay in milliseconds between entering buffering state and displaying the loading spinner. Set null (default) to disable it.
512+
final int? progressIndicatorDelayMS;
513+
508514
static ChewieController of(BuildContext context) {
509515
final chewieControllerProvider =
510516
context.dependOnInheritedWidgetOfExactType<ChewieControllerProvider>()!;

lib/src/material/material_controls.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,22 @@ class _MaterialControlsState extends State<MaterialControls>
560560
void _updateState() {
561561
if (!mounted) return;
562562

563-
if (controller.value.isBuffering) {
564-
timerInstance ??= Timer(const Duration(milliseconds: 200), handleTimeout);
563+
// display the progress bar indicator only after the buffering delay if it has been set
564+
if (chewieController.progressIndicatorDelayMS != null) {
565+
if (controller.value.isBuffering) {
566+
timerInstance ??= Timer(
567+
Duration(
568+
milliseconds: chewieController.progressIndicatorDelayMS!,
569+
),
570+
handleTimeout,
571+
);
572+
} else {
573+
timerInstance?.cancel();
574+
timerInstance = null;
575+
displayLoading = false;
576+
}
565577
} else {
566-
timerInstance?.cancel();
567-
timerInstance = null;
568-
displayLoading = false;
578+
displayLoading = controller.value.isBuffering;
569579
}
570580

571581
setState(() {

0 commit comments

Comments
 (0)