optimize native player with immediate latency, auto seek-to-live-edge, and app resume recovery

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Tommy Chow 2026-03-08 17:05:20 -07:00
parent 418c51ce6d
commit 6e5da0bae0
2 changed files with 31 additions and 22 deletions

View file

@ -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<void> _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

View file

@ -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"