mirror of
https://github.com/block/buzz.git
synced 2026-11-24 02:02:07 +00:00
fix(mobile): keep the agent poster over a failed iOS player (#9693)
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
This commit is contained in:
@@ -78,36 +78,17 @@ class PresetAgentAvatar extends HookWidget {
|
||||
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.
|
||||
// keep the poster under it until playback advances in this showing.
|
||||
final shownFrom = useState<(Duration, Duration)?>(null);
|
||||
useEffect(() {
|
||||
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 &&
|
||||
// A failed player resets its position to zero and stops polling.
|
||||
when !controller.value.hasError &&
|
||||
controller.value.position != fresh &&
|
||||
controller.value.position != polled) {
|
||||
presented.value = true;
|
||||
}
|
||||
@@ -140,20 +121,42 @@ class PresetAgentAvatar extends HookWidget {
|
||||
unawaited(controller.dispose().catchError((Object _) {}));
|
||||
};
|
||||
}, [controller]);
|
||||
final playback = useRef<Future<void>>(Future.value());
|
||||
useEffect(() {
|
||||
if (controller != null && ready.value == controller) {
|
||||
unawaited(
|
||||
(playing ? controller.play() : controller.pause()).catchError((
|
||||
Object _,
|
||||
) {
|
||||
if (context.mounted && ready.value == controller) {
|
||||
ready.value = null;
|
||||
}
|
||||
}),
|
||||
);
|
||||
playback.value = (playing ? controller.play() : controller.pause())
|
||||
.catchError((Object _) {
|
||||
if (context.mounted && ready.value == controller) {
|
||||
ready.value = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}, [controller, ready.value, playing]);
|
||||
// The baseline is read from the player once the play command lands,
|
||||
// since play() seeks a finished loop back to zero. 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.
|
||||
useEffect(() {
|
||||
shownFrom.value = null;
|
||||
// While paused (an inactive app), wait for the play() that resumes it.
|
||||
if (!isIos || !videoShown || !playing || presented.value) return null;
|
||||
var active = true;
|
||||
unawaited(
|
||||
playback.value
|
||||
.then((_) => controller.position)
|
||||
.then((position) {
|
||||
if (active) {
|
||||
shownFrom.value = (
|
||||
position ?? Duration.zero,
|
||||
controller.value.position,
|
||||
);
|
||||
}
|
||||
})
|
||||
.catchError((Object _) {}),
|
||||
);
|
||||
return () => active = false;
|
||||
}, [videoShown, playing, controller]);
|
||||
|
||||
final nativeVideoVisible =
|
||||
isIos &&
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:buzz/shared/widgets/dm_header_portrait.dart';
|
||||
import 'package:buzz/shared/widgets/preset_agent_avatar.dart';
|
||||
import 'package:buzz/shared/widgets/avatar_image.dart';
|
||||
@@ -16,15 +18,27 @@ class _Player extends VideoPlayerPlatform {
|
||||
Future<void> init() async {}
|
||||
@override
|
||||
Future<int?> createWithOptions(VideoCreationOptions options) async => 1;
|
||||
|
||||
/// The newest player's events, for injecting playback errors.
|
||||
StreamController<VideoEvent>? events;
|
||||
@override
|
||||
Stream<VideoEvent> videoEventsFor(int id) {
|
||||
return Stream.value(
|
||||
VideoEvent(
|
||||
eventType: VideoEventType.initialized,
|
||||
duration: const Duration(seconds: 2),
|
||||
size: const Size(100, 100),
|
||||
),
|
||||
);
|
||||
final events = this.events = StreamController<VideoEvent>()
|
||||
..add(
|
||||
VideoEvent(
|
||||
eventType: VideoEventType.initialized,
|
||||
duration: const Duration(seconds: 2),
|
||||
size: const Size(100, 100),
|
||||
),
|
||||
);
|
||||
return events.stream;
|
||||
}
|
||||
|
||||
/// Seeks land after a platform round trip, as on AVFoundation.
|
||||
@override
|
||||
Future<void> seekTo(int id, Duration position) async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1));
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -247,22 +261,85 @@ void main() {
|
||||
// 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) {
|
||||
// Step real frames from the video's mount: the baseline lands before the
|
||||
// first poll does.
|
||||
Future<void> returnCovered({void Function()? onMount}) async {
|
||||
navigator.pop();
|
||||
for (
|
||||
var frame = 0;
|
||||
frame < 60 && find.byKey(const Key('video')).evaluate().isEmpty;
|
||||
frame++
|
||||
) {
|
||||
await tester.pump(const Duration(milliseconds: 16));
|
||||
}
|
||||
expect(find.byKey(const Key('video')), findsOneWidget);
|
||||
for (var frame = 0; frame < 60; frame++) {
|
||||
await tester.pump(const Duration(milliseconds: 16));
|
||||
if (frame == 0) onMount?.call();
|
||||
expect(videoOpacity(), 0);
|
||||
expect(posterOpacity(), 1);
|
||||
}
|
||||
}
|
||||
expect(find.byKey(const Key('video')), findsOneWidget);
|
||||
|
||||
Future<void> cover() async {
|
||||
navigator.push<void>(
|
||||
PageRouteBuilder<void>(
|
||||
opaque: false,
|
||||
pageBuilder: (_, _, _) => const SizedBox.shrink(),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('video')), findsNothing);
|
||||
expect(posterOpacity(), 1);
|
||||
}
|
||||
|
||||
await returnCovered();
|
||||
player.position = const Duration(milliseconds: 800);
|
||||
await tester.pump(const Duration(milliseconds: 600));
|
||||
await tester.pumpAndSettle();
|
||||
expect(videoOpacity(), 1);
|
||||
expect(posterOpacity(), 0);
|
||||
|
||||
// A loop that finished just before the cover restarts from zero on
|
||||
// return, which is not yet playback advancing.
|
||||
player.position = const Duration(seconds: 2);
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
await cover();
|
||||
await returnCovered();
|
||||
player.position = const Duration(milliseconds: 100);
|
||||
await tester.pump(const Duration(milliseconds: 600));
|
||||
await tester.pumpAndSettle();
|
||||
expect(videoOpacity(), 1);
|
||||
expect(posterOpacity(), 0);
|
||||
|
||||
// The same while the app is inactive: the restart waits for resume.
|
||||
player.position = const Duration(seconds: 2);
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
await cover();
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
||||
await returnCovered();
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
for (var frame = 0; frame < 60; frame++) {
|
||||
await tester.pump(const Duration(milliseconds: 16));
|
||||
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);
|
||||
|
||||
// A player that fails on return never shows; the poster stays.
|
||||
await cover();
|
||||
await returnCovered(
|
||||
onMount: () => player.events!.addError(
|
||||
PlatformException(code: 'failed', message: 'playback failed'),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(posterOpacity(), 1);
|
||||
|
||||
// A new character covers with its own poster, never the previous one
|
||||
// kept by gapless playback.
|
||||
final previous = tester.state(find.byType(AvatarImageContent));
|
||||
|
||||
Reference in New Issue
Block a user