146 lines
4.3 KiB
Dart
146 lines
4.3 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
import '../../../ui/adaptive/host.dart';
|
|
|
|
/// Creates the player for one video url; tests inject a fake controller.
|
|
typedef ArtifactVideoControllerFactory =
|
|
VideoPlayerController Function(Uri uri);
|
|
|
|
VideoPlayerController defaultArtifactVideoController(Uri uri) =>
|
|
VideoPlayerController.networkUrl(uri);
|
|
|
|
const String artifactVideoPlayLabel = 'Play video';
|
|
const String artifactVideoPauseLabel = 'Pause video';
|
|
const String artifactVideoError = 'The video could not be played.';
|
|
|
|
/// Plays one recorded same-origin video in the viewer, as the web viewer's
|
|
/// `<video controls>`: it never autoplays, and the operator starts it.
|
|
class ArtifactVideoPlayer extends StatefulWidget {
|
|
const ArtifactVideoPlayer({
|
|
super.key,
|
|
required this.uri,
|
|
this.controllerFactory = defaultArtifactVideoController,
|
|
});
|
|
|
|
final Uri uri;
|
|
final ArtifactVideoControllerFactory controllerFactory;
|
|
|
|
@override
|
|
State<ArtifactVideoPlayer> createState() => _ArtifactVideoPlayerState();
|
|
}
|
|
|
|
class _ArtifactVideoPlayerState extends State<ArtifactVideoPlayer> {
|
|
late VideoPlayerController _controller;
|
|
bool _failed = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_open();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ArtifactVideoPlayer oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.uri != widget.uri) {
|
|
_controller.dispose();
|
|
_open();
|
|
}
|
|
}
|
|
|
|
void _open() {
|
|
_failed = false;
|
|
final controller = widget.controllerFactory(widget.uri);
|
|
_controller = controller;
|
|
controller.initialize().then(
|
|
(_) {
|
|
if (mounted && identical(controller, _controller)) setState(() {});
|
|
},
|
|
onError: (Object _) {
|
|
if (mounted && identical(controller, _controller)) {
|
|
setState(() => _failed = true);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _toggle() {
|
|
final playing = _controller.value.isPlaying;
|
|
playing ? _controller.pause() : _controller.play();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_failed) return const Center(child: Text(artifactVideoError));
|
|
final cupertino = usesCupertinoHost(context);
|
|
return ValueListenableBuilder<VideoPlayerValue>(
|
|
valueListenable: _controller,
|
|
builder: (context, value, _) {
|
|
if (value.hasError) {
|
|
return const Center(child: Text(artifactVideoError));
|
|
}
|
|
if (!value.isInitialized) {
|
|
return Center(
|
|
child: cupertino
|
|
? const CupertinoActivityIndicator()
|
|
: const CircularProgressIndicator(),
|
|
);
|
|
}
|
|
final label = value.isPlaying
|
|
? artifactVideoPauseLabel
|
|
: artifactVideoPlayLabel;
|
|
final icon = cupertino
|
|
? (value.isPlaying
|
|
? CupertinoIcons.pause_fill
|
|
: CupertinoIcons.play_fill)
|
|
: (value.isPlaying ? Icons.pause : Icons.play_arrow);
|
|
return Column(
|
|
key: const Key('artifact-video'),
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Flexible(
|
|
child: AspectRatio(
|
|
aspectRatio: value.aspectRatio,
|
|
child: VideoPlayer(_controller),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
|
child: Row(
|
|
children: [
|
|
cupertino
|
|
? CupertinoButton(
|
|
key: const Key('artifact-video-toggle'),
|
|
onPressed: _toggle,
|
|
child: Icon(icon, semanticLabel: label),
|
|
)
|
|
: IconButton(
|
|
key: const Key('artifact-video-toggle'),
|
|
tooltip: label,
|
|
onPressed: _toggle,
|
|
icon: Icon(icon),
|
|
),
|
|
Expanded(
|
|
child: VideoProgressIndicator(
|
|
_controller,
|
|
allowScrubbing: true,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|