ente/lib/ui/video_widget.dart

191 lines
5.2 KiB
Dart
Raw Normal View History

import 'dart:io' as io;
2020-06-19 23:03:26 +00:00
import 'package:chewie/chewie.dart';
import 'package:flutter/cupertino.dart';
2020-06-20 23:51:10 +00:00
import 'package:flutter/material.dart';
2020-06-19 23:03:26 +00:00
import 'package:flutter/widgets.dart';
2020-07-08 15:39:49 +00:00
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logging/logging.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
import 'package:photos/ui/thumbnail_widget.dart';
2020-06-20 23:51:10 +00:00
import 'package:photos/ui/video_controls.dart';
2020-08-13 20:03:29 +00:00
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/toast_util.dart';
2020-06-19 23:03:26 +00:00
import 'package:video_player/video_player.dart';
2020-07-29 19:17:03 +00:00
import 'package:visibility_detector/visibility_detector.dart';
2020-06-19 23:03:26 +00:00
class VideoWidget extends StatefulWidget {
final File file;
final bool autoPlay;
final String tagPrefix;
2021-07-06 23:24:19 +00:00
final Function(bool) playbackCallback;
VideoWidget(
this.file, {
this.autoPlay = false,
this.tagPrefix,
2021-07-06 23:24:19 +00:00
this.playbackCallback,
Key key,
}) : super(key: key);
2020-06-19 23:03:26 +00:00
@override
_VideoWidgetState createState() => _VideoWidgetState();
}
class _VideoWidgetState extends State<VideoWidget> {
2021-10-06 10:48:38 +00:00
final _logger = Logger("VideoWidget");
2020-06-19 23:03:26 +00:00
VideoPlayerController _videoPlayerController;
ChewieController _chewieController;
double _progress;
2021-07-06 23:24:19 +00:00
bool _isPlaying;
2020-06-19 23:03:26 +00:00
@override
void initState() {
super.initState();
2021-07-19 05:37:34 +00:00
if (widget.file.isRemoteFile()) {
_loadNetworkVideo();
} else if (widget.file.isSharedMediaToAppSandbox()) {
final localFile = io.File(getSharedMediaFilePath(widget.file));
if (localFile.existsSync()) {
2021-09-20 12:27:12 +00:00
_logger.fine("loading from app cache");
_setVideoPlayerController(file: localFile);
} else if (widget.file.uploadedFileID != null) {
_loadNetworkVideo();
}
} else {
widget.file.getAsset().then((asset) async {
if (asset == null || !(await asset.exists)) {
if (widget.file.uploadedFileID != null) {
_loadNetworkVideo();
2020-08-13 20:03:29 +00:00
}
} else {
asset.getMediaUrl().then((url) {
_setVideoPlayerController(url: url);
});
}
});
}
}
void _loadNetworkVideo() {
getFileFromServer(
widget.file,
progressCallback: (count, total) {
if (mounted) {
setState(() {
_progress = count / total;
if (_progress == 1) {
showToast("decrypting video...", toastLength: Toast.LENGTH_SHORT);
}
});
}
},
).then((file) {
if (file != null) {
_setVideoPlayerController(file: file);
}
});
2020-06-19 23:03:26 +00:00
}
@override
void dispose() {
if (_videoPlayerController != null) {
_videoPlayerController.dispose();
}
if (_chewieController != null) {
_chewieController.dispose();
}
2020-06-19 23:03:26 +00:00
super.dispose();
}
VideoPlayerController _setVideoPlayerController({String url, io.File file}) {
2021-10-06 10:48:38 +00:00
VideoPlayerController videoPlayerController;
if (url != null) {
videoPlayerController = VideoPlayerController.network(url);
} else {
videoPlayerController = VideoPlayerController.file(file);
}
return _videoPlayerController = videoPlayerController
..initialize().whenComplete(() {
if (mounted) {
setState(() {});
}
});
}
2020-06-19 23:03:26 +00:00
@override
Widget build(BuildContext context) {
2020-06-23 15:05:48 +00:00
final content = _videoPlayerController != null &&
2021-03-17 19:19:11 +00:00
_videoPlayerController.value.isInitialized
? _getVideoPlayer()
: _getLoadingWidget();
2020-07-29 19:17:03 +00:00
return VisibilityDetector(
key: Key(widget.file.tag()),
onVisibilityChanged: (info) {
if (info.visibleFraction < 1) {
if (mounted && _chewieController != null) {
_chewieController.pause();
}
2020-07-29 19:17:03 +00:00
}
},
child: Hero(
tag: widget.tagPrefix + widget.file.tag(),
child: content,
),
2020-06-23 15:05:48 +00:00
);
}
Widget _getLoadingWidget() {
return Stack(children: [
_getThumbnail(),
Container(
color: Colors.black12,
constraints: BoxConstraints.expand(),
),
Center(
child: SizedBox.fromSize(
size: Size.square(30),
child: _progress == null || _progress == 1
? CupertinoActivityIndicator()
: CircularProgressIndicator(
value: _progress,
valueColor: AlwaysStoppedAnimation<Color>(
Color.fromRGBO(45, 194, 98, 1.0),
),
),
),
),
]);
}
Widget _getThumbnail() {
return Container(
2020-08-13 20:03:29 +00:00
child: ThumbnailWidget(
widget.file,
fit: BoxFit.contain,
),
constraints: BoxConstraints.expand(),
);
2020-06-19 23:03:26 +00:00
}
Widget _getVideoPlayer() {
2021-07-06 23:24:19 +00:00
_videoPlayerController.addListener(() {
if (_isPlaying != _videoPlayerController.value.isPlaying) {
_isPlaying = _videoPlayerController.value.isPlaying;
if (widget.playbackCallback != null) {
widget.playbackCallback(_isPlaying);
}
}
});
2020-06-20 22:47:09 +00:00
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
aspectRatio: _videoPlayerController.value.aspectRatio,
autoPlay: widget.autoPlay,
2020-06-20 22:47:09 +00:00
autoInitialize: true,
looping: true,
allowFullScreen: false,
2020-06-20 23:51:10 +00:00
customControls: VideoControls(),
2020-06-20 22:47:09 +00:00
);
return Chewie(controller: _chewieController);
2020-06-19 23:03:26 +00:00
}
}