From 46590b5a8ddcdc2408c4107923e46c5707edb5fd Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 27 Dec 2021 14:42:06 +0100 Subject: [PATCH] fix: Videoplayer --- lib/pages/chat/events/video_player.dart | 101 +++++++++++++++--------- lib/utils/resize_image.dart | 34 ++++---- 2 files changed, 77 insertions(+), 58 deletions(-) diff --git a/lib/pages/chat/events/video_player.dart b/lib/pages/chat/events/video_player.dart index 8d5fd8f1..3b06636b 100644 --- a/lib/pages/chat/events/video_player.dart +++ b/lib/pages/chat/events/video_player.dart @@ -47,6 +47,23 @@ class _EventVideoPlayerState extends State { } _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) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(e.toLocalizedString(context)), @@ -57,6 +74,8 @@ class _EventVideoPlayerState extends State { )); SentryController.captureException(e, s); } finally { + // Workaround for Chewie needs time to get the aspectRatio + await Future.delayed(const Duration(milliseconds: 100)); setState(() => _isDownloading = false); } } @@ -69,54 +88,58 @@ class _EventVideoPlayerState extends State { static const String fallbackBlurHash = 'L5H2EC=PM+yV0g-mq.wG9c010J}I'; + List _additionalOptions(BuildContext context) => [ + OptionItem( + onTap: () {}, + iconData: Icons.download_outlined, + title: L10n.of(context)!.downloadFile, + ), + ]; + @override Widget build(BuildContext context) { final hasThumbnail = widget.event.hasThumbnail; final blurHash = (widget.event.infoMap as Map) .tryGet('xyz.amorgan.blurhash') ?? 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; - return SizedBox( - width: 400, - height: 300, - child: Stack( - children: [ - if (chewieManager == null) ...[ - if (hasThumbnail) - ImageBubble(widget.event) - else - BlurHash(hash: blurHash), - Center( - child: OutlinedButton.icon( - style: OutlinedButton.styleFrom( - backgroundColor: Theme.of(context).colorScheme.surface, - ), - icon: _isDownloading - ? const CircularProgressIndicator.adaptive(strokeWidth: 2) - : const Icon(Icons.download_outlined), - label: Text( - L10n.of(context)! - .videoWithSize(widget.event.sizeString ?? '?MB'), - ), - onPressed: _isDownloading ? null : _downloadAction, + return Material( + child: SizedBox( + width: 400, + height: 300, + child: chewieManager != null + ? Center(child: Chewie(controller: chewieManager)) + : Stack( + children: [ + if (hasThumbnail) + ImageBubble(widget.event) + else + BlurHash(hash: blurHash), + Center( + child: OutlinedButton.icon( + style: OutlinedButton.styleFrom( + backgroundColor: Theme.of(context).colorScheme.surface, + ), + icon: _isDownloading + ? const SizedBox( + width: 24, + height: 24, + child: CircularProgressIndicator.adaptive( + 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))), - ], ), ); } diff --git a/lib/utils/resize_image.dart b/lib/utils/resize_image.dart index 359650b8..4140e5b2 100644 --- a/lib/utils/resize_image.dart +++ b/lib/utils/resize_image.dart @@ -19,28 +19,24 @@ extension ResizeImage on MatrixFile { static const int max = 1200; static const int quality = 40; - Future resizeVideo() async { - if (!PlatformInfos.isMobile) return this; + Future resizeVideo() async { final tmpDir = await getTemporaryDirectory(); final tmpFile = File(tmpDir.path + name); - if (await tmpFile.exists() == false) { - await tmpFile.writeAsBytes(bytes); - try { - final mediaInfo = await VideoCompress.compressVideo(tmpFile.path); - if (mediaInfo == null) return this; - return MatrixVideoFile( - 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); - } + MediaInfo? mediaInfo; + await tmpFile.writeAsBytes(bytes); + try { + mediaInfo = await VideoCompress.compressVideo(tmpFile.path); + } 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 getVideoThumbnail() async {