Skip to content

Commit a05a72d

Browse files
committed
Add workaround for invalid buffering info on android
1 parent 0a390f9 commit a05a72d

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

lib/src/cupertino/cupertino_controls.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:io';
23
import 'dart:math' as math;
34
import 'dart:ui' as ui;
45

@@ -810,9 +811,28 @@ class _CupertinoControlsState extends State<CupertinoControls>
810811
void _updateState() {
811812
if (!mounted) return;
812813

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+
final int buffer = value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
822+
final int position = value.position.inMilliseconds;
823+
824+
buffering = position >= buffer;
825+
} else {
826+
// -> No buffering
827+
buffering = false;
828+
}
829+
} else {
830+
buffering = controller.value.isBuffering;
831+
}
832+
813833
// display the progress bar indicator only after the buffering delay if it has been set
814834
if (chewieController.progressIndicatorDelay != null) {
815-
if (controller.value.isBuffering) {
835+
if (buffering) {
816836
_bufferingDisplayTimer ??= Timer(
817837
chewieController.progressIndicatorDelay!,
818838
_bufferingTimerTimeout,
@@ -823,7 +843,7 @@ class _CupertinoControlsState extends State<CupertinoControls>
823843
_displayBufferingIndicator = false;
824844
}
825845
} else {
826-
_displayBufferingIndicator = controller.value.isBuffering;
846+
_displayBufferingIndicator = buffering;
827847
}
828848

829849
setState(() {

lib/src/material/material_controls.dart

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

34
import 'package:chewie/src/center_play_button.dart';
45
import 'package:chewie/src/center_seek_button.dart';
@@ -645,9 +646,28 @@ class _MaterialControlsState extends State<MaterialControls>
645646
void _updateState() {
646647
if (!mounted) return;
647648

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+
final int buffer = value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
657+
final int position = value.position.inMilliseconds;
658+
659+
buffering = position >= buffer;
660+
} else {
661+
// -> No buffering
662+
buffering = false;
663+
}
664+
} else {
665+
buffering = controller.value.isBuffering;
666+
}
667+
648668
// display the progress bar indicator only after the buffering delay if it has been set
649669
if (chewieController.progressIndicatorDelay != null) {
650-
if (controller.value.isBuffering) {
670+
if (buffering) {
651671
_bufferingDisplayTimer ??= Timer(
652672
chewieController.progressIndicatorDelay!,
653673
_bufferingTimerTimeout,
@@ -658,7 +678,7 @@ class _MaterialControlsState extends State<MaterialControls>
658678
_displayBufferingIndicator = false;
659679
}
660680
} else {
661-
_displayBufferingIndicator = controller.value.isBuffering;
681+
_displayBufferingIndicator = buffering;
662682
}
663683

664684
setState(() {

lib/src/material/material_desktop_controls.dart

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

34
import 'package:chewie/src/animated_play_pause.dart';
45
import 'package:chewie/src/center_play_button.dart';
@@ -581,9 +582,28 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
581582
void _updateState() {
582583
if (!mounted) return;
583584

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+
final int buffer = value.buffered.lastOrNull?.end.inMilliseconds ?? -1;
593+
final int position = value.position.inMilliseconds;
594+
595+
buffering = position >= buffer;
596+
} else {
597+
// -> No buffering
598+
buffering = false;
599+
}
600+
} else {
601+
buffering = controller.value.isBuffering;
602+
}
603+
584604
// display the progress bar indicator only after the buffering delay if it has been set
585605
if (chewieController.progressIndicatorDelay != null) {
586-
if (controller.value.isBuffering) {
606+
if (buffering) {
587607
_bufferingDisplayTimer ??= Timer(
588608
chewieController.progressIndicatorDelay!,
589609
_bufferingTimerTimeout,
@@ -594,7 +614,7 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
594614
_displayBufferingIndicator = false;
595615
}
596616
} else {
597-
_displayBufferingIndicator = controller.value.isBuffering;
617+
_displayBufferingIndicator = buffering;
598618
}
599619

600620
setState(() {

0 commit comments

Comments
 (0)