diff --git a/mobile/lib/shared/widgets/preset_agent_avatar.dart b/mobile/lib/shared/widgets/preset_agent_avatar.dart index ecc3f32bef..25f495d3c1 100644 --- a/mobile/lib/shared/widgets/preset_agent_avatar.dart +++ b/mobile/lib/shared/widgets/preset_agent_avatar.dart @@ -75,24 +75,44 @@ class PresetAgentAvatar extends HookWidget { [videoUrl, isAndroid], ); final ready = useState(null); - final presented = useState(null); - useListenable( - isIos && posterPlaceholder != null && presented.value != controller - ? controller - : null, - ); + final videoShown = + showNativeVideo && controller != null && ready.value == controller; + // A fresh AVPlayerLayer draws nothing until its first frame arrives, so + // keep the poster under it until playback advances in this showing. The + // baseline is read from the player, and the controller's last polled + // position is kept beside it: while paused that value lags the player, + // so it differs from the baseline until a poll in this showing. + final shownFrom = useState<(Duration, Duration)?>(null); useEffect(() { - if (isIos && - posterPlaceholder != null && - controller != null && - ready.value == controller && - controller.value.position > Duration.zero) { - presented.value = controller; + shownFrom.value = null; + if (!videoShown) return null; + var active = true; + unawaited( + controller.position + .then((position) { + if (active) { + shownFrom.value = ( + position ?? Duration.zero, + controller.value.position, + ); + } + }) + .catchError((Object _) {}), + ); + return () => active = false; + }, [videoShown, controller]); + final presented = useState(false); + useListenable(isIos && videoShown && !presented.value ? controller : null); + useEffect(() { + if (!videoShown) { + presented.value = false; + } else if (shownFrom.value case (final fresh, final polled) + when controller.value.position != fresh && + controller.value.position != polled) { + presented.value = true; } return null; - }, [controller, controller?.value.position]); - final hasPlaybackFrame = - controller != null && presented.value == controller; + }, [videoShown, controller, shownFrom.value, controller?.value.position]); final lifecycle = useAppLifecycleState(); final playing = showNativeVideo && @@ -135,6 +155,18 @@ class PresetAgentAvatar extends HookWidget { return null; }, [controller, ready.value, playing]); + // Keyed by URL so a new character never shows the previous one's poster + // while its own loads. + final posterImage = KeyedSubtree( + key: ValueKey(poster), + child: + posterPlaceholder ?? + AvatarImageContent( + imageUrl: poster, + preferThumbnail: true, + fallback: fallback, + ), + ); return SizedBox.square( dimension: size, child: AvatarClip( @@ -146,41 +178,39 @@ class PresetAgentAvatar extends HookWidget { key: ValueKey(videoUrl), url: videoUrl, playing: playing, - fallback: - posterPlaceholder ?? - AvatarImageContent( - imageUrl: poster, - preferThumbnail: true, - fallback: fallback, - ), + fallback: posterImage, ) - : showNativeVideo && - controller != null && - ready.value == controller - ? posterPlaceholder == null || !isIos + : !isIos + ? videoShown ? VideoPlayer(controller) - : Stack( - fit: StackFit.expand, - children: [ - Opacity( - opacity: hasPlaybackFrame ? 1 : 0, - child: VideoPlayer(controller), - ), - IgnorePointer( - child: AnimatedOpacity( - opacity: hasPlaybackFrame ? 0 : 1, - duration: const Duration(milliseconds: 160), - child: posterPlaceholder!, - ), - ), - ], - ) - : posterPlaceholder ?? - AvatarImageContent( - imageUrl: poster, - preferThumbnail: true, - fallback: fallback, + : posterImage + : Stack( + fit: StackFit.expand, + children: [ + // The poster stays below the native layer, in Flutter's + // base layer: an overlay above it costs a blank frame. + // It stays mounted under the video so the next showing + // covers at once instead of reloading it. + IgnorePointer( + key: const ValueKey('preset-poster'), + child: AnimatedOpacity( + opacity: videoShown && presented.value ? 0 : 1, + // Cover a hidden player at once; fade only onto + // video. + duration: videoShown && presented.value + ? const Duration(milliseconds: 160) + : Duration.zero, + child: posterImage, + ), ), + if (videoShown) + Opacity( + key: const ValueKey('preset-video'), + opacity: presented.value ? 1 : 0, + child: VideoPlayer(controller), + ), + ], + ), ), ), ); diff --git a/mobile/test/shared/widgets/preset_agent_avatar_test.dart b/mobile/test/shared/widgets/preset_agent_avatar_test.dart index 67c99f5e3c..b547177666 100644 --- a/mobile/test/shared/widgets/preset_agent_avatar_test.dart +++ b/mobile/test/shared/widgets/preset_agent_avatar_test.dart @@ -191,6 +191,105 @@ void main() { debugDefaultTargetPlatformOverride = null; }, ); + testWidgets('iOS keeps the poster over a new player until playback advances', ( + tester, + ) async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + addTearDown(() => debugDefaultTargetPlatformOverride = null); + final original = VideoPlayerPlatform.instance; + final player = _Player(); + VideoPlayerPlatform.instance = player; + addTearDown(() => VideoPlayerPlatform.instance = original); + double posterOpacity() => + tester.widget(find.byType(AnimatedOpacity)).opacity; + double videoOpacity() => tester + .widget( + find.ancestor( + of: find.byKey(const Key('video')), + matching: find.byType(Opacity), + ), + ) + .opacity; + await tester.pumpWidget( + const ProviderScope( + child: MaterialApp( + home: PresetAgentAvatar( + imageUrl: + 'https://dwwgwmfqqjotj.cloudfront.net/avatars/20260925T154625166Z/poster/gloopies/gloopies-13.png', + size: 64, + fallback: Text('A'), + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('video')), findsOneWidget); + expect(videoOpacity(), 0); + expect(posterOpacity(), 1); + player.position = const Duration(milliseconds: 100); + await tester.pump(const Duration(milliseconds: 600)); + await tester.pumpAndSettle(); + expect(videoOpacity(), 1); + expect(posterOpacity(), 0); + + // Returning to the route mounts a new player, which starts covered. + final navigator = tester.state(find.byType(Navigator)); + navigator.push( + PageRouteBuilder( + opaque: false, + pageBuilder: (_, _, _) => const SizedBox.shrink(), + ), + ); + await tester.pump(); + expect(find.byKey(const Key('video')), findsNothing); + expect(posterOpacity(), 1); + // The player is past the last polled position when it comes back; the + // poster waits for playback to advance from where it actually is. + player.position = const Duration(milliseconds: 700); + await tester.pumpAndSettle(); + navigator.pop(); + // Step real frames: the baseline lands before the first poll does. + for (var frame = 0; frame < 60; frame++) { + await tester.pump(const Duration(milliseconds: 16)); + if (find.byKey(const Key('video')).evaluate().isNotEmpty) { + expect(videoOpacity(), 0); + expect(posterOpacity(), 1); + } + } + expect(find.byKey(const Key('video')), findsOneWidget); + player.position = const Duration(milliseconds: 800); + await tester.pump(const Duration(milliseconds: 600)); + await tester.pumpAndSettle(); + expect(videoOpacity(), 1); + expect(posterOpacity(), 0); + + // A new character covers with its own poster, never the previous one + // kept by gapless playback. + final previous = tester.state(find.byType(AvatarImageContent)); + await tester.pumpWidget( + const ProviderScope( + child: MaterialApp( + home: PresetAgentAvatar( + imageUrl: + 'https://dwwgwmfqqjotj.cloudfront.net/avatars/20260925T154625166Z/poster/gloopies/gloopies-14.png', + size: 64, + fallback: Text('A'), + ), + ), + ), + ); + await tester.pump(); + final posters = tester + .widgetList(find.byType(AvatarImageContent)) + .map((poster) => poster.imageUrl); + expect(posters, [endsWith('gloopies-14.png')]); + expect( + tester.state(find.byType(AvatarImageContent)), + isNot(same(previous)), + ); + await tester.pumpWidget(const SizedBox.shrink()); + debugDefaultTargetPlatformOverride = null; + }); testWidgets('Android uses transparent renderer and pauses offscreen', ( tester, ) async {