Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Start creating new controls (play pause button is done), similar to controls of native player.

ashilkndec2ee720297
diff --git a/mobile/lib/ui/viewer/file/video_widget.dart b/mobile/lib/ui/viewer/file/video_widget.dart
index 57b20d10f9..7751cf0147 100644
--- a/mobile/lib/ui/viewer/file/video_widget.dart
+++ b/mobile/lib/ui/viewer/file/video_widget.dart
@@ -4,7 +4,7 @@ import "package:flutter/material.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/events/use_media_kit_for_video.dart";
import "package:photos/models/file/file.dart";
-import "package:photos/ui/viewer/file/video_widget_media_kit.dart";
+import "package:photos/ui/viewer/file/video_widget_media_kit_new.dart";
import "package:photos/ui/viewer/file/video_widget_native.dart";
class VideoWidget extends StatefulWidget {
@@ -53,7 +53,7 @@ class _VideoWidgetState extends State<VideoWidget> {
playbackCallback: widget.playbackCallback,
);
} else {
- return VideoWidgetMediaKit(
+ return VideoWidgetMediaKitNew(
widget.file,
tagPrefix: widget.tagPrefix,
playbackCallback: widget.playbackCallback,
diff --git a/mobile/lib/ui/viewer/file/video_widget_media_kit.dart b/mobile/lib/ui/viewer/file/video_widget_media_kit.dart
index e11bc6ce5f..4ce67981b6 100644
--- a/mobile/lib/ui/viewer/file/video_widget_media_kit.dart
+++ b/mobile/lib/ui/viewer/file/video_widget_media_kit.dart
@@ -39,7 +39,7 @@ class VideoWidgetMediaKit extends StatefulWidget {
class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>
with WidgetsBindingObserver {
- final Logger _logger = Logger("VideoWidgetNew");
+ final Logger _logger = Logger("VideoWidgetMediaKit");
static const verticalMargin = 72.0;
late final player = Player();
VideoController? controller;
@@ -201,7 +201,11 @@ class _VideoWidgetMediaKitState extends State<VideoWidgetMediaKit>
_setVideoController(file.path);
}
}).onError((error, stackTrace) {
- showErrorDialog(context, S.of(context).error, S.of(context).failedToDownloadVideo);
+ showErrorDialog(
+ context,
+ S.of(context).error,
+ S.of(context).failedToDownloadVideo,
+ );
});
}
diff --git a/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart b/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart
new file mode 100644
index 0000000000..7482be3061
--- /dev/null
+++ b/mobile/lib/ui/viewer/file/video_widget_media_kit_new.dart
@@ -0,0 +1,315 @@
+import "dart:async";
+import "dart:io";
+
+import "package:flutter/cupertino.dart";
+import "package:flutter/material.dart";
+import "package:logging/logging.dart";
+import "package:media_kit/media_kit.dart";
+import "package:media_kit_video/media_kit_video.dart";
+import "package:photos/core/event_bus.dart";
+import "package:photos/events/guest_view_event.dart";
+import "package:photos/events/pause_video_event.dart";
+import "package:photos/generated/l10n.dart";
+import "package:photos/models/file/extensions/file_props.dart";
+import "package:photos/models/file/file.dart";
+import "package:photos/services/files_service.dart";
+import "package:photos/theme/colors.dart";
+import "package:photos/ui/viewer/file/thumbnail_widget.dart";
+import "package:photos/utils/dialog_util.dart";
+import "package:photos/utils/file_util.dart";
+import "package:photos/utils/toast_util.dart";
+
+class VideoWidgetMediaKitNew extends StatefulWidget {
+ final EnteFile file;
+ final String? tagPrefix;
+ final Function(bool)? playbackCallback;
+ const VideoWidgetMediaKitNew(
+ this.file, {
+ this.tagPrefix,
+ this.playbackCallback,
+ super.key,
+ });
+
+ @override
+ State<VideoWidgetMediaKitNew> createState() => _VideoWidgetMediaKitNewState();
+}
+
+class _VideoWidgetMediaKitNewState extends State<VideoWidgetMediaKitNew>
+ with WidgetsBindingObserver {
+ final Logger _logger = Logger("VideoWidgetMediaKitNew");
+ static const verticalMargin = 72.0;
+ late final player = Player();
+ VideoController? controller;
+ final _progressNotifier = ValueNotifier<double?>(null);
+ late StreamSubscription<bool> playingStreamSubscription;
+ bool _isAppInFG = true;
+ late StreamSubscription<PauseVideoEvent> pauseVideoSubscription;
+ bool isGuestView = false;
+ late final StreamSubscription<GuestViewEvent> _guestViewEventSubscription;
+
+ @override
+ void initState() {
+ _logger.info(
+ 'initState for ${widget.file.generatedID} with tag ${widget.file.tag} and name ${widget.file.displayName}',
+ );
+ super.initState();
+ WidgetsBinding.instance.addObserver(this);
+ if (widget.file.isRemoteFile) {
+ _loadNetworkVideo();
+ _setFileSizeIfNull();
+ } else if (widget.file.isSharedMediaToAppSandbox) {
+ final localFile = File(getSharedMediaFilePath(widget.file));
+ if (localFile.existsSync()) {
+ _setVideoController(localFile.path);
+ } 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();
+ }
+ } else {
+ // ignore: unawaited_futures
+ asset.getMediaUrl().then((url) {
+ _setVideoController(
+ url ??
+ 'https://user-images.githubusercontent.com/28951144/229373695-22f88f13-d18f-4288-9bf1-c3e078d83722.mp4',
+ );
+ });
+ }
+ });
+ }
+ playingStreamSubscription = player.stream.playing.listen((event) {
+ if (widget.playbackCallback != null && mounted) {
+ widget.playbackCallback!(event);
+ }
+ });
+
+ pauseVideoSubscription = Bus.instance.on<PauseVideoEvent>().listen((event) {
+ player.pause();
+ });
+ _guestViewEventSubscription =
+ Bus.instance.on<GuestViewEvent>().listen((event) {
+ setState(() {
+ isGuestView = event.isGuestView;
+ });
+ });
+ }
+
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) {
+ if (state == AppLifecycleState.resumed) {
+ _isAppInFG = true;
+ } else {
+ _isAppInFG = false;
+ }
+ }
+
+ @override
+ void dispose() {
+ _guestViewEventSubscription.cancel();
+ pauseVideoSubscription.cancel();
+ removeCallBack(widget.file);
+ _progressNotifier.dispose();
+ WidgetsBinding.instance.removeObserver(this);
+ playingStreamSubscription.cancel();
+ player.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Center(
+ child: controller != null
+ ? Video(
+ controller: controller!,
+ controls: (state) {
+ return Stack(
+ alignment: Alignment.center,
+ children: [
+ PlayPauseButtonMediaKit(controller),
+ ],
+ );
+ },
+ )
+ : _getLoadingWidget(),
+ );
+ }
+
+ void _loadNetworkVideo() {
+ getFileFromServer(
+ widget.file,
+ progressCallback: (count, total) {
+ if (!mounted) {
+ return;
+ }
+ _progressNotifier.value = count / (widget.file.fileSize ?? total);
+ if (_progressNotifier.value == 1) {
+ if (mounted) {
+ showShortToast(context, S.of(context).decryptingVideo);
+ }
+ }
+ },
+ ).then((file) {
+ if (file != null) {
+ _setVideoController(file.path);
+ }
+ }).onError((error, stackTrace) {
+ showErrorDialog(
+ context,
+ S.of(context).error,
+ S.of(context).failedToDownloadVideo,
+ );
+ });
+ }
+
+ void _setFileSizeIfNull() {
+ if (widget.file.fileSize == null && widget.file.canEditMetaInfo) {
+ FilesService.instance
+ .getFileSize(widget.file.uploadedFileID!)
+ .then((value) {
+ widget.file.fileSize = value;
+ if (mounted) {
+ setState(() {});
+ }
+ });
+ }
+ }
+
+ void _setVideoController(String url) {
+ if (mounted) {
+ setState(() {
+ player.setPlaylistMode(PlaylistMode.single);
+ controller = VideoController(player);
+ player.open(Media(url), play: _isAppInFG);
+ });
+ }
+ }
+
+ Widget _getLoadingWidget() {
+ return Stack(
+ children: [
+ _getThumbnail(),
+ Container(
+ color: Colors.black12,
+ constraints: const BoxConstraints.expand(),
+ ),
+ Center(
+ child: SizedBox.fromSize(
+ size: const Size.square(20),
+ child: ValueListenableBuilder(
+ valueListenable: _progressNotifier,
+ builder: (BuildContext context, double? progress, _) {
+ return progress == null || progress == 1
+ ? const CupertinoActivityIndicator(
+ color: Colors.white,
+ )
+ : CircularProgressIndicator(
+ backgroundColor: Colors.black,
+ value: progress,
+ valueColor: const AlwaysStoppedAnimation<Color>(
+ Color.fromRGBO(45, 194, 98, 1.0),
+ ),
+ );
+ },
+ ),
+ ),
+ ),
+ ],
+ );
+ }
+
+ Widget _getThumbnail() {
+ return Container(
+ color: Colors.black,
+ constraints: const BoxConstraints.expand(),
+ child: ThumbnailWidget(
+ widget.file,
+ fit: BoxFit.contain,
+ ),
+ );
+ }
+}
+
+class PlayPauseButtonMediaKit extends StatefulWidget {
+ final VideoController? controller;
+ const PlayPauseButtonMediaKit(
+ this.controller, {
+ super.key,
+ });
+
+ @override
+ State<PlayPauseButtonMediaKit> createState() => _PlayPauseButtonState();
+}
+
+class _PlayPauseButtonState extends State<PlayPauseButtonMediaKit> {
+ bool _isPlaying = true;
+ late final StreamSubscription<bool>? isPlayingStreamSubscription;
+
+ @override
+ void initState() {
+ super.initState();
+
+ isPlayingStreamSubscription =
+ widget.controller?.player.stream.playing.listen((isPlaying) {
+ setState(() {
+ _isPlaying = isPlaying;
+ });
+ });
+ }
+
+ @override
+ void dispose() {
+ isPlayingStreamSubscription?.cancel();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: () {
+ if (widget.controller?.player.state.playing ?? false) {
+ widget.controller?.player.pause();
+ } else {
+ widget.controller?.player.play();
+ }
+ },
+ child: Container(
+ width: 54,
+ height: 54,
+ decoration: BoxDecoration(
+ color: Colors.black.withOpacity(0.3),
+ shape: BoxShape.circle,
+ border: Border.all(
+ color: strokeFaintDark,
+ width: 1,
+ ),
+ ),
+ child: AnimatedSwitcher(
+ duration: const Duration(milliseconds: 250),
+ transitionBuilder: (Widget child, Animation<double> animation) {
+ return ScaleTransition(scale: animation, child: child);
+ },
+ switchInCurve: Curves.easeInOutQuart,
+ switchOutCurve: Curves.easeInOutQuart,
+ child: _isPlaying
+ ? const Icon(
+ Icons.pause,
+ size: 32,
+ key: ValueKey("pause"),
+ color: Colors.white,
+ )
+ : const Icon(
+ Icons.play_arrow,
+ size: 36,
+ key: ValueKey("play"),
+ color: Colors.white,
+ ),
+ ),
+ ),
+ );
+ }
+}