fix: Videoplayer

This commit is contained in:
Christian Pauly 2021-12-27 14:42:06 +01:00
parent 872deb6578
commit ea1939317c
2 changed files with 77 additions and 58 deletions

View file

@ -47,6 +47,23 @@ class _EventVideoPlayerState extends State<EventVideoPlayer> {
} }
_tmpFile = file; _tmpFile = file;
} }
final tmpFile = _tmpFile;
final networkUri = _networkUri;
if (kIsWeb && networkUri != null && _chewieManager == null) {
_chewieManager ??= ChewieController(
videoPlayerController: VideoPlayerController.network(networkUri),
autoPlay: true,
additionalOptions: _additionalOptions,
autoInitialize: true,
);
} else if (!kIsWeb && tmpFile != null && _chewieManager == null) {
_chewieManager ??= ChewieController(
videoPlayerController: VideoPlayerController.file(tmpFile),
autoPlay: true,
additionalOptions: _additionalOptions,
autoInitialize: true,
);
}
} on MatrixConnectionException catch (e) { } on MatrixConnectionException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar( ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(e.toLocalizedString(context)), content: Text(e.toLocalizedString(context)),
@ -57,6 +74,8 @@ class _EventVideoPlayerState extends State<EventVideoPlayer> {
)); ));
SentryController.captureException(e, s); SentryController.captureException(e, s);
} finally { } finally {
// Workaround for Chewie needs time to get the aspectRatio
await Future.delayed(const Duration(milliseconds: 100));
setState(() => _isDownloading = false); setState(() => _isDownloading = false);
} }
} }
@ -69,54 +88,58 @@ class _EventVideoPlayerState extends State<EventVideoPlayer> {
static const String fallbackBlurHash = 'L5H2EC=PM+yV0g-mq.wG9c010J}I'; static const String fallbackBlurHash = 'L5H2EC=PM+yV0g-mq.wG9c010J}I';
List<OptionItem> _additionalOptions(BuildContext context) => [
OptionItem(
onTap: () {},
iconData: Icons.download_outlined,
title: L10n.of(context)!.downloadFile,
),
];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final hasThumbnail = widget.event.hasThumbnail; final hasThumbnail = widget.event.hasThumbnail;
final blurHash = (widget.event.infoMap as Map<String, dynamic>) final blurHash = (widget.event.infoMap as Map<String, dynamic>)
.tryGet<String>('xyz.amorgan.blurhash') ?? .tryGet<String>('xyz.amorgan.blurhash') ??
fallbackBlurHash; fallbackBlurHash;
final videoFile = _tmpFile;
final networkUri = _networkUri;
if (kIsWeb && networkUri != null && _chewieManager == null) {
_chewieManager = ChewieController(
videoPlayerController: VideoPlayerController.network(networkUri),
);
} else if (!kIsWeb && videoFile != null && _chewieManager == null) {
_chewieManager = ChewieController(
videoPlayerController: VideoPlayerController.file(videoFile),
autoPlay: true,
);
}
final chewieManager = _chewieManager; final chewieManager = _chewieManager;
return SizedBox( return Material(
width: 400, child: SizedBox(
height: 300, width: 400,
child: Stack( height: 300,
children: [ child: chewieManager != null
if (chewieManager == null) ...[ ? Center(child: Chewie(controller: chewieManager))
if (hasThumbnail) : Stack(
ImageBubble(widget.event) children: [
else if (hasThumbnail)
BlurHash(hash: blurHash), ImageBubble(widget.event)
Center( else
child: OutlinedButton.icon( BlurHash(hash: blurHash),
style: OutlinedButton.styleFrom( Center(
backgroundColor: Theme.of(context).colorScheme.surface, child: OutlinedButton.icon(
), style: OutlinedButton.styleFrom(
icon: _isDownloading backgroundColor: Theme.of(context).colorScheme.surface,
? const CircularProgressIndicator.adaptive(strokeWidth: 2) ),
: const Icon(Icons.download_outlined), icon: _isDownloading
label: Text( ? const SizedBox(
L10n.of(context)! width: 24,
.videoWithSize(widget.event.sizeString ?? '?MB'), height: 24,
), child: CircularProgressIndicator.adaptive(
onPressed: _isDownloading ? null : _downloadAction, strokeWidth: 2),
)
: const Icon(Icons.download_outlined),
label: Text(
_isDownloading
? L10n.of(context)!.loadingPleaseWait
: L10n.of(context)!.videoWithSize(
widget.event.sizeString ?? '?MB'),
),
onPressed: _isDownloading ? null : _downloadAction,
),
),
],
), ),
),
] else
Material(child: Center(child: Chewie(controller: chewieManager))),
],
), ),
); );
} }

View file

@ -19,28 +19,24 @@ extension ResizeImage on MatrixFile {
static const int max = 1200; static const int max = 1200;
static const int quality = 40; static const int quality = 40;
Future<MatrixFile> resizeVideo() async { Future<MatrixVideoFile> resizeVideo() async {
if (!PlatformInfos.isMobile) return this;
final tmpDir = await getTemporaryDirectory(); final tmpDir = await getTemporaryDirectory();
final tmpFile = File(tmpDir.path + name); final tmpFile = File(tmpDir.path + name);
if (await tmpFile.exists() == false) { MediaInfo? mediaInfo;
await tmpFile.writeAsBytes(bytes); await tmpFile.writeAsBytes(bytes);
try { try {
final mediaInfo = await VideoCompress.compressVideo(tmpFile.path); mediaInfo = await VideoCompress.compressVideo(tmpFile.path);
if (mediaInfo == null) return this; } catch (e, s) {
return MatrixVideoFile( SentryController.captureException(e, s);
bytes: await tmpFile.readAsBytes(),
name: name,
mimeType: mimeType,
width: mediaInfo.width,
height: mediaInfo.height,
duration: mediaInfo.duration?.round(),
);
} catch (e, s) {
SentryController.captureException(e, s);
}
} }
return this; return MatrixVideoFile(
bytes: await tmpFile.readAsBytes(),
name: name,
mimeType: mimeType,
width: mediaInfo?.width,
height: mediaInfo?.height,
duration: mediaInfo?.duration?.round(),
);
} }
Future<MatrixImageFile?> getVideoThumbnail() async { Future<MatrixImageFile?> getVideoThumbnail() async {