video_widget.dart 7.1 KB

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