26e68140fd
The native video player requests media itself, without the session cookie the app's file fetches send, so a video shown with show_file (or opened from Files) behind the web login was refused in the phone and Mac apps. Pass ServerSession.headersFor(uri) as the player's HTTP headers.
147 lines
4.5 KiB
Dart
147 lines
4.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
import '../../../core/session/server_session.dart';
|
|
|
|
/// Creates the player for one media url; tests inject a fake controller.
|
|
typedef ArtifactVideoControllerFactory =
|
|
VideoPlayerController Function(Uri uri);
|
|
|
|
/// The native players request the media themselves, outside the app's own
|
|
/// fetch, so they carry the same sign-in the app's file fetches send (the web
|
|
/// login's session cookie for a trusted server). A browser's `<video>` sends
|
|
/// its cookie itself and ignores these headers.
|
|
VideoPlayerController defaultArtifactVideoController(Uri uri) =>
|
|
VideoPlayerController.networkUrl(
|
|
uri,
|
|
httpHeaders: ServerSession.headersFor(uri),
|
|
);
|
|
|
|
const String artifactVideoPlayLabel = 'Play video';
|
|
const String artifactVideoPauseLabel = 'Pause video';
|
|
const String artifactAudioPlayLabel = 'Play audio';
|
|
const String artifactAudioPauseLabel = 'Pause audio';
|
|
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,
|
|
this.audio = false,
|
|
});
|
|
|
|
final Uri uri;
|
|
final ArtifactVideoControllerFactory controllerFactory;
|
|
final bool audio;
|
|
|
|
@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));
|
|
return ValueListenableBuilder<VideoPlayerValue>(
|
|
valueListenable: _controller,
|
|
builder: (context, value, _) {
|
|
if (value.hasError) {
|
|
return const Center(child: Text(artifactVideoError));
|
|
}
|
|
if (!value.isInitialized) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
final label = value.isPlaying
|
|
? (widget.audio ? artifactAudioPauseLabel : artifactVideoPauseLabel)
|
|
: (widget.audio ? artifactAudioPlayLabel : artifactVideoPlayLabel);
|
|
final icon = value.isPlaying ? Icons.pause : Icons.play_arrow;
|
|
return Column(
|
|
key: Key(widget.audio ? 'artifact-audio' : 'artifact-video'),
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (!widget.audio)
|
|
Flexible(
|
|
child: AspectRatio(
|
|
aspectRatio: value.aspectRatio,
|
|
child: VideoPlayer(_controller),
|
|
),
|
|
)
|
|
else
|
|
const Padding(
|
|
padding: EdgeInsets.all(24),
|
|
child: Icon(Icons.audio_file_outlined, size: 64),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
|
child: Row(
|
|
children: [
|
|
IconButton(
|
|
key: const Key('artifact-video-toggle'),
|
|
tooltip: label,
|
|
onPressed: _toggle,
|
|
icon: Icon(icon),
|
|
),
|
|
Expanded(
|
|
child: VideoProgressIndicator(
|
|
_controller,
|
|
allowScrubbing: true,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|