video_widget.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import 'dart:async';
  2. import 'dart:io' as io;
  3. import 'package:chewie/chewie.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:logging/logging.dart';
  7. import 'package:photos/core/configuration.dart';
  8. import 'package:photos/core/constants.dart';
  9. import 'package:photos/models/file.dart';
  10. import 'package:photos/services/files_service.dart';
  11. import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
  12. import 'package:photos/ui/viewer/file/video_controls.dart';
  13. import 'package:photos/utils/file_util.dart';
  14. import 'package:photos/utils/toast_util.dart';
  15. import 'package:video_player/video_player.dart';
  16. import 'package:visibility_detector/visibility_detector.dart';
  17. import 'package:wakelock/wakelock.dart';
  18. class VideoWidget extends StatefulWidget {
  19. final File file;
  20. final bool? autoPlay;
  21. final String? tagPrefix;
  22. final Function(bool)? playbackCallback;
  23. const VideoWidget(
  24. this.file, {
  25. this.autoPlay = false,
  26. this.tagPrefix,
  27. this.playbackCallback,
  28. Key? key,
  29. }) : super(key: key);
  30. @override
  31. State<VideoWidget> createState() => _VideoWidgetState();
  32. }
  33. class _VideoWidgetState extends State<VideoWidget> {
  34. final _logger = Logger("VideoWidget");
  35. VideoPlayerController? _videoPlayerController;
  36. ChewieController? _chewieController;
  37. double? _progress;
  38. bool _isPlaying = false;
  39. bool _wakeLockEnabledHere = false;
  40. @override
  41. void initState() {
  42. super.initState();
  43. if (widget.file.isRemoteFile) {
  44. _loadNetworkVideo();
  45. _setFileSizeIfNull();
  46. } else if (widget.file.isSharedMediaToAppSandbox) {
  47. final localFile = io.File(getSharedMediaFilePath(widget.file));
  48. if (localFile.existsSync()) {
  49. _logger.fine("loading from app cache");
  50. _setVideoPlayerController(file: localFile);
  51. } else if (widget.file.uploadedFileID != null) {
  52. _loadNetworkVideo();
  53. }
  54. } else {
  55. widget.file.getAsset.then((asset) async {
  56. if (asset == null || !(await asset.exists)) {
  57. if (widget.file.uploadedFileID != null) {
  58. _loadNetworkVideo();
  59. }
  60. } else {
  61. asset.getMediaUrl().then((url) {
  62. _setVideoPlayerController(url: url);
  63. });
  64. }
  65. });
  66. }
  67. }
  68. void _setFileSizeIfNull() {
  69. if (widget.file.fileSize == null &&
  70. widget.file.ownerID == Configuration.instance.getUserID()) {
  71. FilesService.instance
  72. .getFileSize(widget.file.uploadedFileID!)
  73. .then((value) {
  74. widget.file.fileSize = value;
  75. if (mounted) {
  76. setState(() {});
  77. }
  78. });
  79. }
  80. }
  81. void _loadNetworkVideo() {
  82. getFileFromServer(
  83. widget.file,
  84. progressCallback: (count, total) {
  85. if (mounted) {
  86. setState(() {
  87. _progress = count / (widget.file.fileSize ?? total);
  88. if (_progress == 1) {
  89. showShortToast(context, "Decrypting video...");
  90. }
  91. });
  92. }
  93. },
  94. ).then((file) {
  95. if (file != null) {
  96. _setVideoPlayerController(file: file);
  97. }
  98. });
  99. }
  100. @override
  101. void dispose() {
  102. if (_videoPlayerController != null) {
  103. _videoPlayerController!.dispose();
  104. }
  105. if (_chewieController != null) {
  106. _chewieController!.dispose();
  107. }
  108. if (_wakeLockEnabledHere) {
  109. unawaited(
  110. Wakelock.enabled.then((isEnabled) {
  111. isEnabled ? Wakelock.disable() : null;
  112. }),
  113. );
  114. }
  115. super.dispose();
  116. }
  117. VideoPlayerController _setVideoPlayerController({
  118. String? url,
  119. io.File? file,
  120. }) {
  121. VideoPlayerController videoPlayerController;
  122. if (url != null) {
  123. videoPlayerController = VideoPlayerController.network(url);
  124. } else {
  125. videoPlayerController = VideoPlayerController.file(file!);
  126. }
  127. return _videoPlayerController = videoPlayerController
  128. ..initialize().whenComplete(() {
  129. if (mounted) {
  130. setState(() {});
  131. }
  132. });
  133. }
  134. @override
  135. Widget build(BuildContext context) {
  136. final content = _videoPlayerController != null &&
  137. _videoPlayerController!.value.isInitialized
  138. ? _getVideoPlayer()
  139. : _getLoadingWidget();
  140. final contentWithDetector = GestureDetector(
  141. child: content,
  142. onVerticalDragUpdate: (d) => {
  143. if (d.delta.dy > dragSensitivity) {Navigator.of(context).pop()}
  144. },
  145. );
  146. return VisibilityDetector(
  147. key: Key(widget.file.tag),
  148. onVisibilityChanged: (info) {
  149. if (info.visibleFraction < 1) {
  150. if (mounted && _chewieController != null) {
  151. _chewieController!.pause();
  152. }
  153. }
  154. },
  155. child: Hero(
  156. tag: widget.tagPrefix! + widget.file.tag,
  157. child: contentWithDetector,
  158. ),
  159. );
  160. }
  161. Widget _getLoadingWidget() {
  162. return Stack(
  163. children: [
  164. _getThumbnail(),
  165. Container(
  166. color: Colors.black12,
  167. constraints: const BoxConstraints.expand(),
  168. ),
  169. Center(
  170. child: SizedBox.fromSize(
  171. size: const Size.square(30),
  172. child: _progress == null || _progress == 1
  173. ? const CupertinoActivityIndicator(
  174. color: Colors.white,
  175. )
  176. : CircularProgressIndicator(
  177. backgroundColor: Colors.black,
  178. value: _progress,
  179. valueColor: const AlwaysStoppedAnimation<Color>(
  180. Color.fromRGBO(45, 194, 98, 1.0),
  181. ),
  182. ),
  183. ),
  184. ),
  185. ],
  186. );
  187. }
  188. Widget _getThumbnail() {
  189. return Container(
  190. color: Colors.black,
  191. constraints: const BoxConstraints.expand(),
  192. child: ThumbnailWidget(
  193. widget.file,
  194. fit: BoxFit.contain,
  195. ),
  196. );
  197. }
  198. Future<void> _keepScreenAliveOnPlaying(bool isPlaying) async {
  199. if (isPlaying) {
  200. return Wakelock.enabled.then((value) {
  201. if (value == false) {
  202. Wakelock.enable();
  203. //wakeLockEnabledHere will not be set to true if wakeLock is already enabled from settings on iOS.
  204. //We shouldn't disable when video is not playing if it was enabled manually by the user from ente settings by user.
  205. _wakeLockEnabledHere = true;
  206. }
  207. });
  208. }
  209. if (_wakeLockEnabledHere && !isPlaying) {
  210. return Wakelock.disable();
  211. }
  212. }
  213. Widget _getVideoPlayer() {
  214. _videoPlayerController!.addListener(() {
  215. if (_isPlaying != _videoPlayerController!.value.isPlaying) {
  216. _isPlaying = _videoPlayerController!.value.isPlaying;
  217. if (widget.playbackCallback != null) {
  218. widget.playbackCallback!(_isPlaying);
  219. }
  220. unawaited(_keepScreenAliveOnPlaying(_isPlaying));
  221. }
  222. });
  223. _chewieController = ChewieController(
  224. videoPlayerController: _videoPlayerController!,
  225. aspectRatio: _videoPlayerController!.value.aspectRatio,
  226. autoPlay: widget.autoPlay!,
  227. autoInitialize: true,
  228. looping: true,
  229. allowMuting: true,
  230. allowFullScreen: false,
  231. customControls: const VideoControls(),
  232. );
  233. return Container(
  234. color: Colors.black,
  235. child: Chewie(controller: _chewieController!),
  236. );
  237. }
  238. }