diff --git a/lib/screens/channel/video/native_video_store.dart b/lib/screens/channel/video/native_video_store.dart index 9f7928b0..e0ebf2c9 100644 --- a/lib/screens/channel/video/native_video_store.dart +++ b/lib/screens/channel/video/native_video_store.dart @@ -225,29 +225,37 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { if (settingsStore.autoSyncChatDelay) { settingsStore.syncedChatDelay = 0; } + _pollLatency(); _latencyTimer = Timer.periodic( const Duration(seconds: 10), - (_) async { - final seconds = await _controller?.getLatencyToLive(); - if (seconds == null) return; - final rounded = seconds.round(); - - runInAction(() { - _latency = '${rounded}s'; - }); - - if (!settingsStore.autoSyncChatDelay) return; - - // Only update when unset or drifted by >2s to avoid restarting - // the chat countdown on minor fluctuations. - final current = settingsStore.syncedChatDelay; - if (current == 0 || (seconds - current).abs() > 2) { - settingsStore.syncedChatDelay = seconds; - } - }, + (_) => _pollLatency(), ); } + Future _pollLatency() async { + final seconds = await _controller?.getLatencyToLive(); + if (seconds == null) return; + final rounded = seconds.round(); + + runInAction(() { + _latency = '${rounded}s'; + }); + + // Auto seek to live edge if we've drifted too far. + if (seconds > 15) { + await _controller?.seekToLiveEdge(); + } + + if (!settingsStore.autoSyncChatDelay) return; + + // Only update when unset or drifted by >2s to avoid restarting + // the chat countdown on minor fluctuations. + final current = settingsStore.syncedChatDelay; + if (current == 0 || (seconds - current).abs() > 2) { + settingsStore.syncedChatDelay = seconds; + } + } + void _scheduleOverlayHide([Duration delay = const Duration(seconds: 5)]) { _overlayTimer?.cancel(); @@ -428,9 +436,10 @@ abstract class NativeVideoStoreBase with Store implements VideoPlayerInterface { @override @action void handleAppResume() { - if (!settingsStore.showVideo) { - updateStreamInfo(forceUpdate: true); - } + updateStreamInfo(forceUpdate: true); + // Re-check latency immediately — the stream may have drifted or died + // while backgrounded. If latency > 15s, _pollLatency auto-seeks to live edge. + _pollLatency(); } @override diff --git a/pubspec.lock b/pubspec.lock index 99037401..39ce1e56 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -102,7 +102,7 @@ packages: description: path: "." ref: frosty-patches - resolved-ref: e16914aff5ea8a0f5bc038eb706764738f2a99c9 + resolved-ref: "90b262e9f0d5df797918128e9a277daab57bc814" url: "https://github.com/tommyxchow/flutter-native-video-player.git" source: git version: "0.4.11"