chore: Fix videoplayer in stories

This commit is contained in:
Christian Pauly 2021-12-28 20:04:26 +01:00
parent b571f84991
commit bc07cc28e0
2 changed files with 31 additions and 9 deletions

View File

@ -181,7 +181,7 @@ class StoryPageController extends State<StoryPage> {
}
});
final max = _videoPlayerController?.value.duration ?? maxProgress;
if (progress > max) {
if (progress >= max) {
skip();
}
});
@ -196,14 +196,26 @@ class StoryPageController extends State<StoryPage> {
String get roomId => VRouter.of(context).pathParameters['roomid'] ?? '';
Future<VideoPlayerController>? loadVideoControllerFuture;
Future<VideoPlayerController> loadVideoController(Event event) async {
try {
final matrixFile = await event.downloadAndDecryptAttachment();
final tmpDirectory = await getTemporaryDirectory();
final file = File(tmpDirectory.path + matrixFile.name);
final videoPlayerController = VideoPlayerController.file(file);
final fileName =
event.content.tryGet<String>('filename') ?? 'unknown_story_video.mp4';
final file = File(tmpDirectory.path + '/' + fileName);
await file.writeAsBytes(matrixFile.bytes);
final videoPlayerController =
_videoPlayerController = VideoPlayerController.file(file);
await videoPlayerController.initialize();
videoPlayerController.play();
await videoPlayerController.play();
return videoPlayerController;
} catch (e, s) {
Logs().w('Unable to load video story. Try again...', e, s);
await Future.delayed(const Duration(seconds: 3));
return loadVideoController(event);
}
}
void skip() {
@ -212,6 +224,9 @@ class StoryPageController extends State<StoryPage> {
return;
}
setState(() {
_videoPlayerController?.dispose();
_videoPlayerController = null;
loadVideoControllerFuture = null;
index++;
});
_restartTimer();
@ -222,6 +237,12 @@ class StoryPageController extends State<StoryPage> {
bool isHold = false;
@override
void dispose() {
_videoPlayerController?.dispose();
super.dispose();
}
void hold([_]) {
_holdedAt = DateTime.now();
if (loadingMode) return;

View File

@ -160,7 +160,8 @@ class StoryView extends StatelessWidget {
if (event.messageType == MessageTypes.Video &&
PlatformInfos.isMobile)
FutureBuilder<VideoPlayerController>(
future: controller.loadVideoController(event),
future: controller.loadVideoControllerFuture ??=
controller.loadVideoController(event),
builder: (context, snapshot) {
final videoPlayerController = snapshot.data;
if (videoPlayerController == null) {