video_widget.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/generated/l10n.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 = false;
  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, S.of(context).decryptingVideo);
  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({
  119. String? url,
  120. io.File? file,
  121. }) {
  122. VideoPlayerController videoPlayerController;
  123. if (url != null) {
  124. videoPlayerController = VideoPlayerController.network(url);
  125. } else {
  126. videoPlayerController = VideoPlayerController.file(file!);
  127. }
  128. return _videoPlayerController = videoPlayerController
  129. ..initialize().whenComplete(() {
  130. if (mounted) {
  131. setState(() {});
  132. }
  133. });
  134. }
  135. @override
  136. Widget build(BuildContext context) {
  137. final content = _videoPlayerController != null &&
  138. _videoPlayerController!.value.isInitialized
  139. ? _getVideoPlayer()
  140. : _getLoadingWidget();
  141. final contentWithDetector = GestureDetector(
  142. child: content,
  143. onVerticalDragUpdate: (d) => {
  144. if (d.delta.dy > dragSensitivity) {Navigator.of(context).pop()}
  145. },
  146. );
  147. return VisibilityDetector(
  148. key: Key(widget.file.tag),
  149. onVisibilityChanged: (info) {
  150. if (info.visibleFraction < 1) {
  151. if (mounted && _chewieController != null) {
  152. _chewieController!.pause();
  153. }
  154. }
  155. },
  156. child: Hero(
  157. tag: widget.tagPrefix! + widget.file.tag,
  158. child: contentWithDetector,
  159. ),
  160. );
  161. }
  162. Widget _getLoadingWidget() {
  163. return Stack(
  164. children: [
  165. _getThumbnail(),
  166. Container(
  167. color: Colors.black12,
  168. constraints: const BoxConstraints.expand(),
  169. ),
  170. Center(
  171. child: SizedBox.fromSize(
  172. size: const Size.square(30),
  173. child: _progress == null || _progress == 1
  174. ? const CupertinoActivityIndicator(
  175. color: Colors.white,
  176. )
  177. : CircularProgressIndicator(
  178. backgroundColor: Colors.black,
  179. value: _progress,
  180. valueColor: const AlwaysStoppedAnimation<Color>(
  181. Color.fromRGBO(45, 194, 98, 1.0),
  182. ),
  183. ),
  184. ),
  185. ),
  186. ],
  187. );
  188. }
  189. Widget _getThumbnail() {
  190. return Container(
  191. color: Colors.black,
  192. constraints: const BoxConstraints.expand(),
  193. child: ThumbnailWidget(
  194. widget.file,
  195. fit: BoxFit.contain,
  196. ),
  197. );
  198. }
  199. Future<void> _keepScreenAliveOnPlaying(bool isPlaying) async {
  200. if (isPlaying) {
  201. return Wakelock.enabled.then((value) {
  202. if (value == false) {
  203. Wakelock.enable();
  204. //wakeLockEnabledHere will not be set to true if wakeLock is already enabled from settings on iOS.
  205. //We shouldn't disable when video is not playing if it was enabled manually by the user from ente settings by user.
  206. _wakeLockEnabledHere = true;
  207. }
  208. });
  209. }
  210. if (_wakeLockEnabledHere && !isPlaying) {
  211. return Wakelock.disable();
  212. }
  213. }
  214. Widget _getVideoPlayer() {
  215. _videoPlayerController!.addListener(() {
  216. if (_isPlaying != _videoPlayerController!.value.isPlaying) {
  217. _isPlaying = _videoPlayerController!.value.isPlaying;
  218. if (widget.playbackCallback != null) {
  219. widget.playbackCallback!(_isPlaying);
  220. }
  221. unawaited(_keepScreenAliveOnPlaying(_isPlaying));
  222. }
  223. });
  224. _chewieController = ChewieController(
  225. videoPlayerController: _videoPlayerController!,
  226. aspectRatio: _videoPlayerController!.value.aspectRatio,
  227. autoPlay: widget.autoPlay!,
  228. autoInitialize: true,
  229. looping: true,
  230. allowMuting: true,
  231. allowFullScreen: false,
  232. customControls: const VideoControls(),
  233. );
  234. return Container(
  235. color: Colors.black,
  236. child: Chewie(controller: _chewieController!),
  237. );
  238. }
  239. }