Skip to content

Commit 45c7fca

Browse files
committed
Moved buffer detection logic for andorid into shared function
1 parent ecea260 commit 45c7fca

File tree

4 files changed

+34
-84
lines changed

4 files changed

+34
-84
lines changed

lib/src/cupertino/cupertino_controls.dart

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'dart:io';
32
import 'dart:math' as math;
43
import 'dart:ui' as ui;
54

@@ -811,33 +810,7 @@ class _CupertinoControlsState extends State<CupertinoControls>
811810
void _updateState() {
812811
if (!mounted) return;
813812

814-
late final bool buffering;
815-
816-
if (Platform.isAndroid) {
817-
if (controller.value.isBuffering) {
818-
// -> Check if we actually buffer, as android has a bug preventing to
819-
// get the correct buffering state from this single bool.
820-
final VideoPlayerValue value = controller.value;
821-
822-
final int position = value.position.inMilliseconds;
823-
824-
// Special case, if the video is finished, we don't want to show the
825-
// buffering indicator anymore
826-
if (position >= value.duration.inMilliseconds) {
827-
buffering = false;
828-
} else {
829-
final int buffer =
830-
value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
831-
832-
buffering = position >= buffer;
833-
}
834-
} else {
835-
// -> No buffering
836-
buffering = false;
837-
}
838-
} else {
839-
buffering = controller.value.isBuffering;
840-
}
813+
final bool buffering = getIsBuffering(controller);
841814

842815
// display the progress bar indicator only after the buffering delay if it has been set
843816
if (chewieController.progressIndicatorDelay != null) {

lib/src/helpers/utils.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import 'dart:io';
2+
3+
import 'package:video_player/video_player.dart';
4+
15
String formatDuration(Duration position) {
26
final ms = position.inMilliseconds;
37

@@ -30,3 +34,30 @@ String formatDuration(Duration position) {
3034

3135
return formattedTime;
3236
}
37+
38+
bool getIsBuffering(VideoPlayerController controller) {
39+
final VideoPlayerValue value = controller.value;
40+
41+
if (Platform.isAndroid) {
42+
if (value.isBuffering) {
43+
// -> Check if we actually buffer, as android has a bug preventing to
44+
// get the correct buffering state from this single bool.
45+
final int position = value.position.inMilliseconds;
46+
47+
// Special case, if the video is finished, we don't want to show the
48+
// buffering indicator anymore
49+
if (position >= value.duration.inMilliseconds) {
50+
return false;
51+
} else {
52+
final int buffer = value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
53+
54+
return position >= buffer;
55+
}
56+
} else {
57+
// -> No buffering
58+
return false;
59+
}
60+
}
61+
62+
return value.isBuffering;
63+
}

lib/src/material/material_controls.dart

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'dart:io';
32

43
import 'package:chewie/src/center_play_button.dart';
54
import 'package:chewie/src/center_seek_button.dart';
@@ -646,33 +645,7 @@ class _MaterialControlsState extends State<MaterialControls>
646645
void _updateState() {
647646
if (!mounted) return;
648647

649-
late final bool buffering;
650-
651-
if (Platform.isAndroid) {
652-
if (controller.value.isBuffering) {
653-
// -> Check if we actually buffer, as android has a bug preventing to
654-
// get the correct buffering state from this single bool.
655-
final VideoPlayerValue value = controller.value;
656-
657-
final int position = value.position.inMilliseconds;
658-
659-
// Special case, if the video is finished, we don't want to show the
660-
// buffering indicator anymore
661-
if (position >= value.duration.inMilliseconds) {
662-
buffering = false;
663-
} else {
664-
final int buffer =
665-
value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
666-
667-
buffering = position >= buffer;
668-
}
669-
} else {
670-
// -> No buffering
671-
buffering = false;
672-
}
673-
} else {
674-
buffering = controller.value.isBuffering;
675-
}
648+
final bool buffering = getIsBuffering(controller);
676649

677650
// display the progress bar indicator only after the buffering delay if it has been set
678651
if (chewieController.progressIndicatorDelay != null) {

lib/src/material/material_desktop_controls.dart

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:async';
2-
import 'dart:io';
32

43
import 'package:chewie/src/animated_play_pause.dart';
54
import 'package:chewie/src/center_play_button.dart';
@@ -582,33 +581,7 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
582581
void _updateState() {
583582
if (!mounted) return;
584583

585-
late final bool buffering;
586-
587-
if (Platform.isAndroid) {
588-
if (controller.value.isBuffering) {
589-
// -> Check if we actually buffer, as android has a bug preventing to
590-
// get the correct buffering state from this single bool.
591-
final VideoPlayerValue value = controller.value;
592-
593-
final int position = value.position.inMilliseconds;
594-
595-
// Special case, if the video is finished, we don't want to show the
596-
// buffering indicator anymore
597-
if (position >= value.duration.inMilliseconds) {
598-
buffering = false;
599-
} else {
600-
final int buffer =
601-
value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
602-
603-
buffering = position >= buffer;
604-
}
605-
} else {
606-
// -> No buffering
607-
buffering = false;
608-
}
609-
} else {
610-
buffering = controller.value.isBuffering;
611-
}
584+
final bool buffering = getIsBuffering(controller);
612585

613586
// display the progress bar indicator only after the buffering delay if it has been set
614587
if (chewieController.progressIndicatorDelay != null) {

0 commit comments

Comments
 (0)