2024-02-23 07:18:02 +02:00
|
|
|
import 'package:chewie/chewie.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
|
|
|
|
/// Provides the initialized video player controller
|
|
|
|
/// If the asset is local, use the local file
|
|
|
|
/// Otherwise, use a video player with a URL
|
2024-03-06 05:42:22 +02:00
|
|
|
ChewieController useChewieController({
|
|
|
|
required VideoPlayerController controller,
|
2024-02-23 07:18:02 +02:00
|
|
|
EdgeInsets controlsSafeAreaMinimum = const EdgeInsets.only(
|
|
|
|
bottom: 100,
|
|
|
|
),
|
|
|
|
bool showOptions = true,
|
|
|
|
bool showControlsOnInitialize = false,
|
|
|
|
bool autoPlay = true,
|
|
|
|
bool allowFullScreen = false,
|
|
|
|
bool allowedScreenSleep = false,
|
|
|
|
bool showControls = true,
|
|
|
|
Widget? customControls,
|
|
|
|
Widget? placeholder,
|
|
|
|
Duration hideControlsTimer = const Duration(seconds: 1),
|
|
|
|
VoidCallback? onPlaying,
|
|
|
|
VoidCallback? onPaused,
|
|
|
|
VoidCallback? onVideoEnded,
|
|
|
|
}) {
|
|
|
|
return use(
|
|
|
|
_ChewieControllerHook(
|
2024-03-06 05:42:22 +02:00
|
|
|
controller: controller,
|
2024-02-23 07:18:02 +02:00
|
|
|
placeholder: placeholder,
|
|
|
|
showOptions: showOptions,
|
|
|
|
controlsSafeAreaMinimum: controlsSafeAreaMinimum,
|
|
|
|
autoPlay: autoPlay,
|
|
|
|
allowFullScreen: allowFullScreen,
|
|
|
|
customControls: customControls,
|
|
|
|
hideControlsTimer: hideControlsTimer,
|
|
|
|
showControlsOnInitialize: showControlsOnInitialize,
|
|
|
|
showControls: showControls,
|
|
|
|
allowedScreenSleep: allowedScreenSleep,
|
|
|
|
onPlaying: onPlaying,
|
|
|
|
onPaused: onPaused,
|
|
|
|
onVideoEnded: onVideoEnded,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-06 05:42:22 +02:00
|
|
|
class _ChewieControllerHook extends Hook<ChewieController> {
|
|
|
|
final VideoPlayerController controller;
|
2024-02-23 07:18:02 +02:00
|
|
|
final EdgeInsets controlsSafeAreaMinimum;
|
|
|
|
final bool showOptions;
|
|
|
|
final bool showControlsOnInitialize;
|
|
|
|
final bool autoPlay;
|
|
|
|
final bool allowFullScreen;
|
|
|
|
final bool allowedScreenSleep;
|
|
|
|
final bool showControls;
|
|
|
|
final Widget? customControls;
|
|
|
|
final Widget? placeholder;
|
|
|
|
final Duration hideControlsTimer;
|
|
|
|
final VoidCallback? onPlaying;
|
|
|
|
final VoidCallback? onPaused;
|
|
|
|
final VoidCallback? onVideoEnded;
|
|
|
|
|
|
|
|
const _ChewieControllerHook({
|
2024-03-06 05:42:22 +02:00
|
|
|
required this.controller,
|
2024-02-23 07:18:02 +02:00
|
|
|
this.controlsSafeAreaMinimum = const EdgeInsets.only(
|
|
|
|
bottom: 100,
|
|
|
|
),
|
|
|
|
this.showOptions = true,
|
|
|
|
this.showControlsOnInitialize = false,
|
|
|
|
this.autoPlay = true,
|
|
|
|
this.allowFullScreen = false,
|
|
|
|
this.allowedScreenSleep = false,
|
|
|
|
this.showControls = true,
|
|
|
|
this.customControls,
|
|
|
|
this.placeholder,
|
|
|
|
this.hideControlsTimer = const Duration(seconds: 3),
|
|
|
|
this.onPlaying,
|
|
|
|
this.onPaused,
|
|
|
|
this.onVideoEnded,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
createState() => _ChewieControllerHookState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChewieControllerHookState
|
2024-03-06 05:42:22 +02:00
|
|
|
extends HookState<ChewieController, _ChewieControllerHook> {
|
|
|
|
late ChewieController chewieController = ChewieController(
|
|
|
|
videoPlayerController: hook.controller,
|
|
|
|
controlsSafeAreaMinimum: hook.controlsSafeAreaMinimum,
|
|
|
|
showOptions: hook.showOptions,
|
|
|
|
showControlsOnInitialize: hook.showControlsOnInitialize,
|
|
|
|
autoPlay: hook.autoPlay,
|
|
|
|
allowFullScreen: hook.allowFullScreen,
|
|
|
|
allowedScreenSleep: hook.allowedScreenSleep,
|
|
|
|
showControls: hook.showControls,
|
|
|
|
customControls: hook.customControls,
|
|
|
|
placeholder: hook.placeholder,
|
|
|
|
hideControlsTimer: hook.hideControlsTimer,
|
|
|
|
);
|
2024-02-23 07:18:02 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-03-06 05:42:22 +02:00
|
|
|
chewieController.dispose();
|
2024-02-23 07:18:02 +02:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-06 05:42:22 +02:00
|
|
|
ChewieController build(BuildContext context) {
|
2024-02-23 07:18:02 +02:00
|
|
|
return chewieController;
|
|
|
|
}
|
|
|
|
|
2024-03-06 05:42:22 +02:00
|
|
|
/*
|
2024-02-23 07:18:02 +02:00
|
|
|
/// Initializes the chewie controller and video player controller
|
|
|
|
Future<void> _initialize() async {
|
|
|
|
if (hook.asset.isLocal && hook.asset.livePhotoVideoId == null) {
|
|
|
|
// Use a local file for the video player controller
|
|
|
|
final file = await hook.asset.local!.file;
|
|
|
|
if (file == null) {
|
|
|
|
throw Exception('No file found for the video');
|
|
|
|
}
|
|
|
|
videoPlayerController = VideoPlayerController.file(file);
|
|
|
|
} else {
|
|
|
|
// Use a network URL for the video player controller
|
|
|
|
final serverEndpoint = store.Store.get(store.StoreKey.serverEndpoint);
|
|
|
|
final String videoUrl = hook.asset.livePhotoVideoId != null
|
|
|
|
? '$serverEndpoint/asset/file/${hook.asset.livePhotoVideoId}'
|
|
|
|
: '$serverEndpoint/asset/file/${hook.asset.remoteId}';
|
|
|
|
|
|
|
|
final url = Uri.parse(videoUrl);
|
|
|
|
final accessToken = store.Store.get(StoreKey.accessToken);
|
|
|
|
|
|
|
|
videoPlayerController = VideoPlayerController.networkUrl(
|
|
|
|
url,
|
|
|
|
httpHeaders: {"x-immich-user-token": accessToken},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await videoPlayerController!.initialize();
|
|
|
|
|
2024-03-06 05:42:22 +02:00
|
|
|
chewieController = ChewieController(
|
|
|
|
videoPlayerController: videoPlayerController!,
|
|
|
|
controlsSafeAreaMinimum: hook.controlsSafeAreaMinimum,
|
|
|
|
showOptions: hook.showOptions,
|
|
|
|
showControlsOnInitialize: hook.showControlsOnInitialize,
|
|
|
|
autoPlay: hook.autoPlay,
|
|
|
|
allowFullScreen: hook.allowFullScreen,
|
|
|
|
allowedScreenSleep: hook.allowedScreenSleep,
|
|
|
|
showControls: hook.showControls,
|
|
|
|
customControls: hook.customControls,
|
|
|
|
placeholder: hook.placeholder,
|
|
|
|
hideControlsTimer: hook.hideControlsTimer,
|
|
|
|
);
|
2024-02-23 07:18:02 +02:00
|
|
|
}
|
2024-03-06 05:42:22 +02:00
|
|
|
*/
|
2024-02-23 07:18:02 +02:00
|
|
|
}
|