Merge branch 'soru/video-player' into 'main'
feat: Add video player Closes #192 See merge request famedly/fluffychat!479onboarding
commit
abab724af9
@ -0,0 +1,93 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:vrouter/vrouter.dart';
|
||||||
|
import 'package:chewie/chewie.dart';
|
||||||
|
import 'package:video_player/video_player.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
|
import 'views/video_viewer_view.dart';
|
||||||
|
import '../widgets/matrix.dart';
|
||||||
|
import '../utils/matrix_sdk_extensions.dart/event_extension.dart';
|
||||||
|
import '../utils/platform_infos.dart';
|
||||||
|
|
||||||
|
class VideoViewer extends StatefulWidget {
|
||||||
|
final Event event;
|
||||||
|
|
||||||
|
const VideoViewer(this.event, {Key key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
VideoViewerController createState() => VideoViewerController();
|
||||||
|
}
|
||||||
|
|
||||||
|
class VideoViewerController extends State<VideoViewer> {
|
||||||
|
VideoPlayerController videoPlayerController;
|
||||||
|
ChewieController chewieController;
|
||||||
|
dynamic error;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
(() async {
|
||||||
|
try {
|
||||||
|
if (widget.event.content['file'] is Map) {
|
||||||
|
if (PlatformInfos.isWeb) {
|
||||||
|
throw 'Encrypted videos unavailable in web';
|
||||||
|
}
|
||||||
|
final tempDirectory = (await getTemporaryDirectory()).path;
|
||||||
|
final mxcUri = widget.event.content
|
||||||
|
.tryGet<Map<String, dynamic>>('file')
|
||||||
|
?.tryGet<String>('url');
|
||||||
|
if (mxcUri == null) {
|
||||||
|
throw 'No mxc uri found';
|
||||||
|
}
|
||||||
|
// somehow the video viewer doesn't like the uri-encoded slashes, so we'll just gonna replace them with hyphons
|
||||||
|
final file = File(
|
||||||
|
'$tempDirectory/videos/${mxcUri.replaceAll(':', '').replaceAll('/', '-')}');
|
||||||
|
if (await file.exists() == false) {
|
||||||
|
final matrixFile =
|
||||||
|
await widget.event.downloadAndDecryptAttachmentCached();
|
||||||
|
await file.create(recursive: true);
|
||||||
|
await file.writeAsBytes(matrixFile.bytes);
|
||||||
|
}
|
||||||
|
videoPlayerController = VideoPlayerController.file(file);
|
||||||
|
} else if (widget.event.content['url'] is String) {
|
||||||
|
videoPlayerController = VideoPlayerController.network(
|
||||||
|
widget.event.getAttachmentUrl()?.toString());
|
||||||
|
} else {
|
||||||
|
throw 'invalid event';
|
||||||
|
}
|
||||||
|
await videoPlayerController.initialize();
|
||||||
|
|
||||||
|
chewieController = ChewieController(
|
||||||
|
videoPlayerController: videoPlayerController,
|
||||||
|
autoPlay: true,
|
||||||
|
looping: false,
|
||||||
|
);
|
||||||
|
setState(() => null);
|
||||||
|
} catch (e) {
|
||||||
|
setState(() => error = e);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
chewieController?.dispose();
|
||||||
|
videoPlayerController?.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Forward this video to another room.
|
||||||
|
void forwardAction() {
|
||||||
|
Matrix.of(context).shareContent = widget.event.content;
|
||||||
|
VRouter.of(context).to('/rooms');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Save this file with a system call.
|
||||||
|
void saveFileAction() => widget.event.saveFile(context);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) => VideoViewerView(this);
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
import '../video_viewer.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
import 'package:chewie/chewie.dart';
|
||||||
|
|
||||||
|
class VideoViewerView extends StatelessWidget {
|
||||||
|
final VideoViewerController controller;
|
||||||
|
|
||||||
|
const VideoViewerView(this.controller, {Key key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
backgroundColor: Colors.black,
|
||||||
|
extendBodyBehindAppBar: true,
|
||||||
|
appBar: AppBar(
|
||||||
|
elevation: 0,
|
||||||
|
leading: IconButton(
|
||||||
|
icon: Icon(Icons.close),
|
||||||
|
onPressed: Navigator.of(context).pop,
|
||||||
|
color: Colors.white,
|
||||||
|
tooltip: L10n.of(context).close,
|
||||||
|
),
|
||||||
|
backgroundColor: Color(0x44000000),
|
||||||
|
actions: [
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(Icons.reply_outlined),
|
||||||
|
onPressed: controller.forwardAction,
|
||||||
|
color: Colors.white,
|
||||||
|
tooltip: L10n.of(context).share,
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(Icons.download_outlined),
|
||||||
|
onPressed: controller.saveFileAction,
|
||||||
|
color: Colors.white,
|
||||||
|
tooltip: L10n.of(context).downloadFile,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body: Center(
|
||||||
|
child: controller.error != null
|
||||||
|
? Text(controller.error.toString())
|
||||||
|
: (controller.chewieController == null
|
||||||
|
? CircularProgressIndicator(strokeWidth: 2)
|
||||||
|
: Chewie(
|
||||||
|
controller: controller.chewieController,
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue