Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background tap to pause video feature #875

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/src/chewie_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class ChewieController extends ChangeNotifier {
this.progressIndicatorDelay,
this.hideControlsTimer = defaultHideControlsTimer,
this.controlsSafeAreaMinimum = EdgeInsets.zero,
this.pauseOnBackgroundTap = false,
}) : assert(
playbackSpeeds.every((speed) => speed > 0),
'The playbackSpeeds values must all be greater than 0',
Expand Down Expand Up @@ -363,6 +364,7 @@ class ChewieController extends ChangeNotifier {
Animation<double>,
ChewieControllerProvider,
)? routePageBuilder,
bool? pauseOnBackgroundTap,
}) {
return ChewieController(
draggableProgressBar: draggableProgressBar ?? this.draggableProgressBar,
Expand Down Expand Up @@ -417,6 +419,7 @@ class ChewieController extends ChangeNotifier {
hideControlsTimer: hideControlsTimer ?? this.hideControlsTimer,
progressIndicatorDelay:
progressIndicatorDelay ?? this.progressIndicatorDelay,
pauseOnBackgroundTap: pauseOnBackgroundTap ?? this.pauseOnBackgroundTap,
);
}

Expand Down Expand Up @@ -575,6 +578,9 @@ class ChewieController extends ChangeNotifier {
/// Defaults to [EdgeInsets.zero].
final EdgeInsets controlsSafeAreaMinimum;

/// Defines if the player should pause when the background is tapped
final bool pauseOnBackgroundTap;

static ChewieController of(BuildContext context) {
final chewieControllerProvider =
context.dependOnInheritedWidgetOfExactType<ChewieControllerProvider>()!;
Expand Down
10 changes: 9 additions & 1 deletion lib/src/cupertino/cupertino_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,15 @@ class _CupertinoControlsState extends State<CupertinoControls>

return GestureDetector(
onTap: _latestValue.isPlaying
? _cancelAndRestartTimer
? _chewieController?.pauseOnBackgroundTap ?? false
? () {
_playPause();

setState(() {
notifier.hideStuff = true;
});
}
: _cancelAndRestartTimer
: () {
_hideTimer?.cancel();

Expand Down
15 changes: 10 additions & 5 deletions lib/src/material/material_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,17 @@ class _MaterialControlsState extends State<MaterialControls>
return GestureDetector(
onTap: () {
if (_latestValue.isPlaying) {
if (_displayTapped) {
setState(() {
notifier.hideStuff = true;
});
} else {
if (_chewieController?.pauseOnBackgroundTap ?? false) {
_playPause();
_cancelAndRestartTimer();
} else {
if (_displayTapped) {
setState(() {
notifier.hideStuff = true;
});
} else {
_cancelAndRestartTimer();
}
}
} else {
_playPause();
Expand Down
15 changes: 10 additions & 5 deletions lib/src/material/material_desktop_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,17 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
return GestureDetector(
onTap: () {
if (_latestValue.isPlaying) {
if (_displayTapped) {
setState(() {
notifier.hideStuff = true;
});
} else {
if (_chewieController?.pauseOnBackgroundTap ?? false) {
_playPause();
_cancelAndRestartTimer();
} else {
if (_displayTapped) {
setState(() {
notifier.hideStuff = true;
});
} else {
_cancelAndRestartTimer();
}
}
} else {
_playPause();
Expand Down