Skip to content

Commit d2bcf5d

Browse files
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
1 parent c8331f1 commit d2bcf5d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

mobile/lib/pages/common/native_video_viewer.page.dart

+18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class NativeVideoViewerPage extends HookConsumerWidget {
4444
final lastVideoPosition = useRef(-1);
4545
final isBuffering = useRef(false);
4646

47+
// Used to track whether the video should play when the app
48+
// is brought back to the foreground
49+
final shouldPlayOnForeground = useRef(true);
50+
4751
// When a video is opened through the timeline, `isCurrent` will immediately be true.
4852
// When swiping from video A to video B, `isCurrent` will initially be true for video A and false for video B.
4953
// If the swipe is completed, `isCurrent` will be true for video B after a delay.
@@ -368,6 +372,20 @@ class NativeVideoViewerPage extends HookConsumerWidget {
368372
const [],
369373
);
370374

375+
useOnAppLifecycleStateChange((_, state) async {
376+
if (state == AppLifecycleState.resumed && shouldPlayOnForeground.value) {
377+
controller.value?.play();
378+
} else if (state == AppLifecycleState.paused) {
379+
final videoPlaying = await controller.value?.isPlaying();
380+
if (videoPlaying ?? true) {
381+
shouldPlayOnForeground.value = true;
382+
controller.value?.pause();
383+
} else {
384+
shouldPlayOnForeground.value = false;
385+
}
386+
}
387+
});
388+
371389
return Stack(
372390
children: [
373391
// This remains under the video to avoid flickering

0 commit comments

Comments
 (0)