2022-11-08 19:00:24 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2024-01-15 18:50:33 +02:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-02-06 08:07:56 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-04-02 19:31:53 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-02-06 08:07:56 +02:00
|
|
|
import 'package:chewie/chewie.dart';
|
2023-11-09 18:19:53 +02:00
|
|
|
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
2022-04-02 19:31:53 +02:00
|
|
|
import 'package:immich_mobile/modules/asset_viewer/models/image_viewer_page_state.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/asset_viewer/providers/image_viewer_page_state.provider.dart';
|
2023-06-26 17:27:47 +02:00
|
|
|
import 'package:immich_mobile/modules/asset_viewer/ui/video_player_controls.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-11-11 19:52:02 +02:00
|
|
|
import 'package:immich_mobile/shared/ui/immich_loading_indicator.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2022-02-06 08:07:56 +02:00
|
|
|
import 'package:video_player/video_player.dart';
|
2023-08-19 00:52:40 +02:00
|
|
|
import 'package:wakelock_plus/wakelock_plus.dart';
|
2022-02-06 08:07:56 +02:00
|
|
|
|
2024-01-15 18:50:33 +02:00
|
|
|
@RoutePage()
|
2022-04-02 19:31:53 +02:00
|
|
|
// ignore: must_be_immutable
|
|
|
|
class VideoViewerPage extends HookConsumerWidget {
|
2022-11-08 19:00:24 +02:00
|
|
|
final Asset asset;
|
2022-11-19 07:12:54 +02:00
|
|
|
final bool isMotionVideo;
|
2023-06-20 23:17:43 +02:00
|
|
|
final Widget? placeholder;
|
2022-11-19 07:12:54 +02:00
|
|
|
final VoidCallback onVideoEnded;
|
2023-02-05 14:57:07 +02:00
|
|
|
final VoidCallback? onPlaying;
|
|
|
|
final VoidCallback? onPaused;
|
2022-02-06 08:07:56 +02:00
|
|
|
|
2022-11-19 07:12:54 +02:00
|
|
|
const VideoViewerPage({
|
2024-01-27 18:14:32 +02:00
|
|
|
super.key,
|
2022-11-19 07:12:54 +02:00
|
|
|
required this.asset,
|
|
|
|
required this.isMotionVideo,
|
|
|
|
required this.onVideoEnded,
|
2023-02-05 14:57:07 +02:00
|
|
|
this.onPlaying,
|
|
|
|
this.onPaused,
|
2023-06-20 23:17:43 +02:00
|
|
|
this.placeholder,
|
2024-01-27 18:14:32 +02:00
|
|
|
});
|
2022-02-06 08:07:56 +02:00
|
|
|
|
|
|
|
@override
|
2022-04-02 19:31:53 +02:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2023-08-02 23:01:30 +02:00
|
|
|
if (asset.isLocal && asset.livePhotoVideoId == null) {
|
2022-11-08 19:00:24 +02:00
|
|
|
final AsyncValue<File> videoFile = ref.watch(_fileFamily(asset.local!));
|
|
|
|
return videoFile.when(
|
2023-04-18 18:23:56 +02:00
|
|
|
data: (data) => VideoPlayer(
|
2022-11-19 07:12:54 +02:00
|
|
|
file: data,
|
|
|
|
isMotionVideo: false,
|
|
|
|
onVideoEnded: () {},
|
|
|
|
),
|
2022-11-08 19:00:24 +02:00
|
|
|
error: (error, stackTrace) => Icon(
|
|
|
|
Icons.image_not_supported_outlined,
|
2023-11-09 18:19:53 +02:00
|
|
|
color: context.primaryColor,
|
2022-11-08 19:00:24 +02:00
|
|
|
),
|
|
|
|
loading: () => const Center(
|
|
|
|
child: SizedBox(
|
|
|
|
width: 75,
|
|
|
|
height: 75,
|
|
|
|
child: CircularProgressIndicator.adaptive(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-06-25 20:46:51 +02:00
|
|
|
final downloadAssetStatus =
|
|
|
|
ref.watch(imageViewerStateProvider).downloadAssetStatus;
|
2022-11-19 07:12:54 +02:00
|
|
|
final String videoUrl = isMotionVideo
|
2023-03-23 03:36:44 +02:00
|
|
|
? '${Store.get(StoreKey.serverEndpoint)}/asset/file/${asset.livePhotoVideoId}'
|
|
|
|
: '${Store.get(StoreKey.serverEndpoint)}/asset/file/${asset.remoteId}';
|
2022-02-06 08:07:56 +02:00
|
|
|
|
2022-08-03 22:36:12 +02:00
|
|
|
return Stack(
|
|
|
|
children: [
|
2023-04-18 18:23:56 +02:00
|
|
|
VideoPlayer(
|
2022-08-03 22:36:12 +02:00
|
|
|
url: videoUrl,
|
2024-02-04 22:35:13 +02:00
|
|
|
accessToken: Store.get(StoreKey.accessToken),
|
2022-11-19 07:12:54 +02:00
|
|
|
isMotionVideo: isMotionVideo,
|
|
|
|
onVideoEnded: onVideoEnded,
|
2023-02-05 14:57:07 +02:00
|
|
|
onPaused: onPaused,
|
|
|
|
onPlaying: onPlaying,
|
2023-06-20 23:17:43 +02:00
|
|
|
placeholder: placeholder,
|
2022-02-06 08:07:56 +02:00
|
|
|
),
|
2022-08-03 22:36:12 +02:00
|
|
|
if (downloadAssetStatus == DownloadAssetStatus.loading)
|
2023-06-27 15:48:20 +02:00
|
|
|
SizedBox(
|
2023-11-09 18:19:53 +02:00
|
|
|
height: context.height,
|
|
|
|
width: context.width,
|
2023-06-27 15:48:20 +02:00
|
|
|
child: const Center(
|
|
|
|
child: ImmichLoadingIndicator(),
|
|
|
|
),
|
2022-08-03 22:36:12 +02:00
|
|
|
),
|
|
|
|
],
|
2022-02-06 08:07:56 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
final _fileFamily =
|
|
|
|
FutureProvider.family<File, AssetEntity>((ref, entity) async {
|
|
|
|
final file = await entity.file;
|
|
|
|
if (file == null) {
|
|
|
|
throw Exception();
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
|
2023-04-18 18:23:56 +02:00
|
|
|
class VideoPlayer extends StatefulWidget {
|
2022-11-08 19:00:24 +02:00
|
|
|
final String? url;
|
2024-02-04 22:35:13 +02:00
|
|
|
final String? accessToken;
|
2022-11-08 19:00:24 +02:00
|
|
|
final File? file;
|
2022-11-19 07:12:54 +02:00
|
|
|
final bool isMotionVideo;
|
|
|
|
final VoidCallback onVideoEnded;
|
|
|
|
|
2023-02-05 14:57:07 +02:00
|
|
|
final Function()? onPlaying;
|
|
|
|
final Function()? onPaused;
|
|
|
|
|
2023-06-20 23:17:43 +02:00
|
|
|
/// The placeholder to show while the video is loading
|
|
|
|
/// usually, a thumbnail of the video
|
|
|
|
final Widget? placeholder;
|
|
|
|
|
2023-04-18 18:23:56 +02:00
|
|
|
const VideoPlayer({
|
2024-01-27 18:14:32 +02:00
|
|
|
super.key,
|
2022-11-19 07:12:54 +02:00
|
|
|
this.url,
|
2024-02-04 22:35:13 +02:00
|
|
|
this.accessToken,
|
2022-11-19 07:12:54 +02:00
|
|
|
this.file,
|
|
|
|
required this.onVideoEnded,
|
|
|
|
required this.isMotionVideo,
|
2023-02-05 14:57:07 +02:00
|
|
|
this.onPlaying,
|
|
|
|
this.onPaused,
|
2023-06-20 23:17:43 +02:00
|
|
|
this.placeholder,
|
2024-01-27 18:14:32 +02:00
|
|
|
});
|
2022-02-06 08:07:56 +02:00
|
|
|
|
|
|
|
@override
|
2023-04-18 18:23:56 +02:00
|
|
|
State<VideoPlayer> createState() => _VideoPlayerState();
|
2022-02-06 08:07:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 18:23:56 +02:00
|
|
|
class _VideoPlayerState extends State<VideoPlayer> {
|
2022-02-06 08:07:56 +02:00
|
|
|
late VideoPlayerController videoPlayerController;
|
|
|
|
ChewieController? chewieController;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
initializePlayer();
|
2022-11-19 07:12:54 +02:00
|
|
|
|
|
|
|
videoPlayerController.addListener(() {
|
2023-04-18 18:23:56 +02:00
|
|
|
if (videoPlayerController.value.isInitialized) {
|
|
|
|
if (videoPlayerController.value.isPlaying) {
|
2023-08-19 00:52:40 +02:00
|
|
|
WakelockPlus.enable();
|
2023-04-18 18:23:56 +02:00
|
|
|
widget.onPlaying?.call();
|
|
|
|
} else if (!videoPlayerController.value.isPlaying) {
|
2023-08-19 00:52:40 +02:00
|
|
|
WakelockPlus.disable();
|
2023-04-18 18:23:56 +02:00
|
|
|
widget.onPaused?.call();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoPlayerController.value.position ==
|
|
|
|
videoPlayerController.value.duration) {
|
2023-08-19 00:52:40 +02:00
|
|
|
WakelockPlus.disable();
|
2023-04-18 18:23:56 +02:00
|
|
|
widget.onVideoEnded();
|
|
|
|
}
|
2022-11-19 07:12:54 +02:00
|
|
|
}
|
|
|
|
});
|
2022-02-06 08:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initializePlayer() async {
|
|
|
|
try {
|
2022-11-08 19:00:24 +02:00
|
|
|
videoPlayerController = widget.file == null
|
2023-08-19 00:52:40 +02:00
|
|
|
? VideoPlayerController.networkUrl(
|
|
|
|
Uri.parse(widget.url!),
|
2024-02-04 22:35:13 +02:00
|
|
|
httpHeaders: {"x-immich-user-token": widget.accessToken ?? ""},
|
2022-11-08 19:00:24 +02:00
|
|
|
)
|
|
|
|
: VideoPlayerController.file(widget.file!);
|
2022-02-06 08:07:56 +02:00
|
|
|
|
|
|
|
await videoPlayerController.initialize();
|
|
|
|
_createChewieController();
|
|
|
|
setState(() {});
|
|
|
|
} catch (e) {
|
2023-04-18 18:23:56 +02:00
|
|
|
debugPrint("ERROR initialize video player $e");
|
2022-02-06 08:07:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_createChewieController() {
|
|
|
|
chewieController = ChewieController(
|
2023-04-06 19:51:52 +02:00
|
|
|
controlsSafeAreaMinimum: const EdgeInsets.only(
|
2023-04-18 18:23:56 +02:00
|
|
|
bottom: 100,
|
2023-04-06 19:51:52 +02:00
|
|
|
),
|
2022-02-06 08:07:56 +02:00
|
|
|
showOptions: true,
|
2022-09-11 07:25:04 +02:00
|
|
|
showControlsOnInitialize: false,
|
2022-02-06 08:07:56 +02:00
|
|
|
videoPlayerController: videoPlayerController,
|
|
|
|
autoPlay: true,
|
2022-08-03 22:36:12 +02:00
|
|
|
autoInitialize: true,
|
2023-06-26 17:27:47 +02:00
|
|
|
allowFullScreen: false,
|
2023-04-18 18:23:56 +02:00
|
|
|
allowedScreenSleep: false,
|
2022-11-19 07:12:54 +02:00
|
|
|
showControls: !widget.isMotionVideo,
|
2023-06-26 17:27:47 +02:00
|
|
|
customControls: const VideoPlayerControls(),
|
2022-08-03 22:36:12 +02:00
|
|
|
hideControlsTimer: const Duration(seconds: 5),
|
2022-02-06 08:07:56 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
videoPlayerController.pause();
|
|
|
|
videoPlayerController.dispose();
|
|
|
|
chewieController?.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-04-18 18:23:56 +02:00
|
|
|
if (chewieController?.videoPlayerController.value.isInitialized == true) {
|
|
|
|
return SizedBox(
|
2024-02-01 00:38:32 +02:00
|
|
|
height: context.height,
|
|
|
|
width: context.width,
|
2023-04-18 18:23:56 +02:00
|
|
|
child: Chewie(
|
|
|
|
controller: chewieController!,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
2023-06-20 23:17:43 +02:00
|
|
|
return SizedBox(
|
2023-11-09 18:19:53 +02:00
|
|
|
height: context.height,
|
|
|
|
width: context.width,
|
2023-06-20 23:17:43 +02:00
|
|
|
child: Center(
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
2023-08-19 00:52:40 +02:00
|
|
|
if (widget.placeholder != null) widget.placeholder!,
|
2023-06-20 23:17:43 +02:00
|
|
|
const Center(
|
|
|
|
child: ImmichLoadingIndicator(),
|
|
|
|
),
|
|
|
|
],
|
2023-04-18 18:23:56 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-02-06 08:07:56 +02:00
|
|
|
}
|
|
|
|
}
|