fix(mobile): pause background video play (#17032)

* fix(mobile): prevent background video playback

* fix: logic for tracking app state

* chore: move lifecycle handler in separate file

* chore: replace useState with useRef

* chore: useOnAppLifecycleStateChange

* fix: removed print statement
This commit is contained in:
Yaros 2025-03-28 16:32:25 +01:00 committed by GitHub
parent c8331f111f
commit d2bcf5d716
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -44,6 +44,10 @@ class NativeVideoViewerPage extends HookConsumerWidget {
final lastVideoPosition = useRef(-1); final lastVideoPosition = useRef(-1);
final isBuffering = useRef(false); final isBuffering = useRef(false);
// Used to track whether the video should play when the app
// is brought back to the foreground
final shouldPlayOnForeground = useRef(true);
// When a video is opened through the timeline, `isCurrent` will immediately be true. // When a video is opened through the timeline, `isCurrent` will immediately be true.
// When swiping from video A to video B, `isCurrent` will initially be true for video A and false for video B. // When swiping from video A to video B, `isCurrent` will initially be true for video A and false for video B.
// If the swipe is completed, `isCurrent` will be true for video B after a delay. // If the swipe is completed, `isCurrent` will be true for video B after a delay.
@ -368,6 +372,20 @@ class NativeVideoViewerPage extends HookConsumerWidget {
const [], const [],
); );
useOnAppLifecycleStateChange((_, state) async {
if (state == AppLifecycleState.resumed && shouldPlayOnForeground.value) {
controller.value?.play();
} else if (state == AppLifecycleState.paused) {
final videoPlaying = await controller.value?.isPlaying();
if (videoPlaying ?? true) {
shouldPlayOnForeground.value = true;
controller.value?.pause();
} else {
shouldPlayOnForeground.value = false;
}
}
});
return Stack( return Stack(
children: [ children: [
// This remains under the video to avoid flickering // This remains under the video to avoid flickering