added autoplay to videos
This commit is contained in:
@@ -34,6 +34,7 @@ class FullScreenMedia extends StatefulWidget {
|
|||||||
String? userName,
|
String? userName,
|
||||||
this.onShowMessage,
|
this.onShowMessage,
|
||||||
this.attachmentActionsModalBuilder,
|
this.attachmentActionsModalBuilder,
|
||||||
|
this.autoplayVideos = false,
|
||||||
}) : userName = userName ?? '',
|
}) : userName = userName ?? '',
|
||||||
super(key: key);
|
super(key: key);
|
||||||
|
|
||||||
@@ -57,6 +58,8 @@ class FullScreenMedia extends StatefulWidget {
|
|||||||
/// Use [defaultActionsModal.copyWith] to easily customize it
|
/// Use [defaultActionsModal.copyWith] to easily customize it
|
||||||
final AttachmentActionsBuilder? attachmentActionsModalBuilder;
|
final AttachmentActionsBuilder? attachmentActionsModalBuilder;
|
||||||
|
|
||||||
|
final bool autoplayVideos;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_FullScreenMediaState createState() => _FullScreenMediaState();
|
_FullScreenMediaState createState() => _FullScreenMediaState();
|
||||||
}
|
}
|
||||||
@@ -81,7 +84,8 @@ class _FullScreenMediaState extends State<FullScreenMedia>
|
|||||||
);
|
);
|
||||||
_pageController = PageController(initialPage: widget.startIndex);
|
_pageController = PageController(initialPage: widget.startIndex);
|
||||||
_currentPage = widget.startIndex;
|
_currentPage = widget.startIndex;
|
||||||
for (final attachment in widget.mediaAttachments) {
|
for (var i = 0; i < widget.mediaAttachments.length; i++) {
|
||||||
|
final attachment = widget.mediaAttachments[i];
|
||||||
if (attachment.type != 'video') continue;
|
if (attachment.type != 'video') continue;
|
||||||
final package = VideoPackage(attachment, showControls: true);
|
final package = VideoPackage(attachment, showControls: true);
|
||||||
videoPackages[attachment.id] = package;
|
videoPackages[attachment.id] = package;
|
||||||
@@ -90,9 +94,21 @@ class _FullScreenMediaState extends State<FullScreenMedia>
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> initializePlayers() async {
|
Future<void> initializePlayers() async {
|
||||||
|
if (videoPackages.isEmpty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final currentAttachment = widget.mediaAttachments[widget.startIndex];
|
||||||
|
|
||||||
await Future.wait(videoPackages.values.map(
|
await Future.wait(videoPackages.values.map(
|
||||||
(it) => it.initialize(),
|
(it) => it.initialize(),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
if (widget.autoplayVideos && currentAttachment.type == 'video') {
|
||||||
|
final package = videoPackages.values
|
||||||
|
.firstWhere((e) => e._attachment == currentAttachment);
|
||||||
|
package._chewieController?.play();
|
||||||
|
}
|
||||||
setState(() {}); // ignore: no-empty-block
|
setState(() {}); // ignore: no-empty-block
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +125,24 @@ class _FullScreenMediaState extends State<FullScreenMedia>
|
|||||||
setState(() {
|
setState(() {
|
||||||
_currentPage = val;
|
_currentPage = val;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (videoPackages.isEmpty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final currentAttachment = widget.mediaAttachments[val];
|
||||||
|
|
||||||
|
for (final e in videoPackages.values) {
|
||||||
|
if (e._attachment != currentAttachment) {
|
||||||
|
e._chewieController?.pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (widget.autoplayVideos &&
|
||||||
|
currentAttachment.type == 'video') {
|
||||||
|
final controller = videoPackages[currentAttachment.id]!;
|
||||||
|
controller._chewieController?.play();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final attachment = widget.mediaAttachments[index];
|
final attachment = widget.mediaAttachments[index];
|
||||||
@@ -243,15 +277,16 @@ class _FullScreenMediaState extends State<FullScreenMedia>
|
|||||||
class VideoPackage {
|
class VideoPackage {
|
||||||
/// Constructor for creating [VideoPackage]
|
/// Constructor for creating [VideoPackage]
|
||||||
VideoPackage(
|
VideoPackage(
|
||||||
Attachment attachment, {
|
this._attachment, {
|
||||||
bool showControls = false,
|
bool showControls = false,
|
||||||
bool autoInitialize = true,
|
bool autoInitialize = true,
|
||||||
}) : _showControls = showControls,
|
}) : _showControls = showControls,
|
||||||
_autoInitialize = autoInitialize,
|
_autoInitialize = autoInitialize,
|
||||||
_videoPlayerController = attachment.localUri != null
|
_videoPlayerController = _attachment.localUri != null
|
||||||
? VideoPlayerController.file(File.fromUri(attachment.localUri!))
|
? VideoPlayerController.file(File.fromUri(_attachment.localUri!))
|
||||||
: VideoPlayerController.network(attachment.assetUrl!);
|
: VideoPlayerController.network(_attachment.assetUrl!);
|
||||||
|
|
||||||
|
final Attachment _attachment;
|
||||||
final bool _showControls;
|
final bool _showControls;
|
||||||
final bool _autoInitialize;
|
final bool _autoInitialize;
|
||||||
final VideoPlayerController _videoPlayerController;
|
final VideoPlayerController _videoPlayerController;
|
||||||
|
|||||||
Reference in New Issue
Block a user