Skip to content

Commit bd70415

Browse files
committed
Workaround for seeking to somewhere in paused state results in infinite loading on android
1 parent ecea260 commit bd70415

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

lib/src/cupertino/cupertino_controls.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:chewie/src/chewie_player.dart';
99
import 'package:chewie/src/chewie_progress_colors.dart';
1010
import 'package:chewie/src/cupertino/cupertino_progress_bar.dart';
1111
import 'package:chewie/src/cupertino/widgets/cupertino_options_dialog.dart';
12+
import 'package:chewie/src/helpers/cancel_token.dart';
1213
import 'package:chewie/src/helpers/utils.dart';
1314
import 'package:chewie/src/models/option_item.dart';
1415
import 'package:chewie/src/models/subtitle_model.dart';
@@ -808,12 +809,19 @@ class _CupertinoControlsState extends State<CupertinoControls>
808809
}
809810
}
810811

812+
CancelToken? _updateStateCancelToken;
813+
811814
void _updateState() {
812815
if (!mounted) return;
813816

814817
late final bool buffering;
815818

816819
if (Platform.isAndroid) {
820+
if (_updateStateCancelToken != null) {
821+
_updateStateCancelToken!.cancel();
822+
_updateStateCancelToken = null;
823+
}
824+
817825
if (controller.value.isBuffering) {
818826
// -> Check if we actually buffer, as android has a bug preventing to
819827
// get the correct buffering state from this single bool.
@@ -835,6 +843,19 @@ class _CupertinoControlsState extends State<CupertinoControls>
835843
// -> No buffering
836844
buffering = false;
837845
}
846+
847+
// We need to check again later, as the buffering state might change
848+
// without notice. This is due to no listener is called as buffering is
849+
// always true.
850+
if (buffering) {
851+
final CancelToken cancelToken = CancelToken();
852+
_updateStateCancelToken = cancelToken;
853+
854+
Future.delayed(const Duration(milliseconds: 100), () {
855+
if (cancelToken.isCancelled) return;
856+
_updateState();
857+
});
858+
}
838859
} else {
839860
buffering = controller.value.isBuffering;
840861
}

lib/src/helpers/cancel_token.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CancelToken {
2+
bool _isCancelled = false;
3+
4+
/// Cancel the request.
5+
void cancel() {
6+
_isCancelled = true;
7+
}
8+
9+
/// Check if the request is cancelled.
10+
bool get isCancelled => _isCancelled;
11+
}

lib/src/material/material_controls.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:chewie/src/center_play_button.dart';
55
import 'package:chewie/src/center_seek_button.dart';
66
import 'package:chewie/src/chewie_player.dart';
77
import 'package:chewie/src/chewie_progress_colors.dart';
8+
import 'package:chewie/src/helpers/cancel_token.dart';
89
import 'package:chewie/src/helpers/utils.dart';
910
import 'package:chewie/src/material/color_compat_extensions.dart';
1011
import 'package:chewie/src/material/material_progress_bar.dart';
@@ -643,12 +644,19 @@ class _MaterialControlsState extends State<MaterialControls>
643644
}
644645
}
645646

647+
CancelToken? _updateStateCancelToken;
648+
646649
void _updateState() {
647650
if (!mounted) return;
648651

649652
late final bool buffering;
650653

651654
if (Platform.isAndroid) {
655+
if (_updateStateCancelToken != null) {
656+
_updateStateCancelToken!.cancel();
657+
_updateStateCancelToken = null;
658+
}
659+
652660
if (controller.value.isBuffering) {
653661
// -> Check if we actually buffer, as android has a bug preventing to
654662
// get the correct buffering state from this single bool.
@@ -670,6 +678,19 @@ class _MaterialControlsState extends State<MaterialControls>
670678
// -> No buffering
671679
buffering = false;
672680
}
681+
682+
// We need to check again later, as the buffering state might change
683+
// without notice. This is due to no listener is called as buffering is
684+
// always true.
685+
if (buffering) {
686+
final CancelToken cancelToken = CancelToken();
687+
_updateStateCancelToken = cancelToken;
688+
689+
Future.delayed(const Duration(milliseconds: 100), () {
690+
if (cancelToken.isCancelled) return;
691+
_updateState();
692+
});
693+
}
673694
} else {
674695
buffering = controller.value.isBuffering;
675696
}

lib/src/material/material_desktop_controls.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:chewie/src/animated_play_pause.dart';
55
import 'package:chewie/src/center_play_button.dart';
66
import 'package:chewie/src/chewie_player.dart';
77
import 'package:chewie/src/chewie_progress_colors.dart';
8+
import 'package:chewie/src/helpers/cancel_token.dart';
89
import 'package:chewie/src/helpers/utils.dart';
910
import 'package:chewie/src/material/color_compat_extensions.dart';
1011
import 'package:chewie/src/material/material_progress_bar.dart';
@@ -579,12 +580,19 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
579580
}
580581
}
581582

583+
CancelToken? _updateStateCancelToken;
584+
582585
void _updateState() {
583586
if (!mounted) return;
584587

585588
late final bool buffering;
586589

587590
if (Platform.isAndroid) {
591+
if (_updateStateCancelToken != null) {
592+
_updateStateCancelToken!.cancel();
593+
_updateStateCancelToken = null;
594+
}
595+
588596
if (controller.value.isBuffering) {
589597
// -> Check if we actually buffer, as android has a bug preventing to
590598
// get the correct buffering state from this single bool.
@@ -606,6 +614,19 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
606614
// -> No buffering
607615
buffering = false;
608616
}
617+
618+
// We need to check again later, as the buffering state might change
619+
// without notice. This is due to no listener is called as buffering is
620+
// always true.
621+
if (buffering) {
622+
final CancelToken cancelToken = CancelToken();
623+
_updateStateCancelToken = cancelToken;
624+
625+
Future.delayed(const Duration(milliseconds: 100), () {
626+
if (cancelToken.isCancelled) return;
627+
_updateState();
628+
});
629+
}
609630
} else {
610631
buffering = controller.value.isBuffering;
611632
}

0 commit comments

Comments
 (0)