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,32 +88,30 @@ 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(
child: SizedBox(
width: 400, width: 400,
height: 300, height: 300,
child: Stack( child: chewieManager != null
? Center(child: Chewie(controller: chewieManager))
: Stack(
children: [ children: [
if (chewieManager == null) ...[
if (hasThumbnail) if (hasThumbnail)
ImageBubble(widget.event) ImageBubble(widget.event)
else else
@ -105,19 +122,25 @@ class _EventVideoPlayerState extends State<EventVideoPlayer> {
backgroundColor: Theme.of(context).colorScheme.surface, backgroundColor: Theme.of(context).colorScheme.surface,
), ),
icon: _isDownloading icon: _isDownloading
? const CircularProgressIndicator.adaptive(strokeWidth: 2) ? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator.adaptive(
strokeWidth: 2),
)
: const Icon(Icons.download_outlined), : const Icon(Icons.download_outlined),
label: Text( label: Text(
L10n.of(context)! _isDownloading
.videoWithSize(widget.event.sizeString ?? '?MB'), ? L10n.of(context)!.loadingPleaseWait
: L10n.of(context)!.videoWithSize(
widget.event.sizeString ?? '?MB'),
), ),
onPressed: _isDownloading ? null : _downloadAction, 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) {
SentryController.captureException(e, s);
}
return MatrixVideoFile( return MatrixVideoFile(
bytes: await tmpFile.readAsBytes(), bytes: await tmpFile.readAsBytes(),
name: name, name: name,
mimeType: mimeType, mimeType: mimeType,
width: mediaInfo.width, width: mediaInfo?.width,
height: mediaInfo.height, height: mediaInfo?.height,
duration: mediaInfo.duration?.round(), duration: mediaInfo?.duration?.round(),
); );
} catch (e, s) {
SentryController.captureException(e, s);
}
}
return this;
} }
Future<MatrixImageFile?> getVideoThumbnail() async { Future<MatrixImageFile?> getVideoThumbnail() async {