2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
|
|
|
import 'package:immich_mobile/entities/store.entity.dart';
|
2024-06-26 21:31:55 +02:00
|
|
|
import 'package:immich_mobile/services/api.service.dart';
|
2024-03-06 04:42:22 +01:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
|
|
|
|
part 'video_player_controller_provider.g.dart';
|
|
|
|
|
|
|
|
@riverpod
|
|
|
|
Future<VideoPlayerController> videoPlayerController(
|
|
|
|
VideoPlayerControllerRef ref, {
|
|
|
|
required Asset asset,
|
|
|
|
}) async {
|
|
|
|
late VideoPlayerController controller;
|
|
|
|
if (asset.isLocal && asset.livePhotoVideoId == null) {
|
|
|
|
// Use a local file for the video player controller
|
|
|
|
final file = await asset.local!.file;
|
|
|
|
if (file == null) {
|
|
|
|
throw Exception('No file found for the video');
|
|
|
|
}
|
|
|
|
controller = VideoPlayerController.file(file);
|
|
|
|
} else {
|
|
|
|
// Use a network URL for the video player controller
|
|
|
|
final serverEndpoint = Store.get(StoreKey.serverEndpoint);
|
|
|
|
final String videoUrl = asset.livePhotoVideoId != null
|
2024-05-31 19:44:04 +02:00
|
|
|
? '$serverEndpoint/assets/${asset.livePhotoVideoId}/video/playback'
|
|
|
|
: '$serverEndpoint/assets/${asset.remoteId}/video/playback';
|
2024-03-06 04:42:22 +01:00
|
|
|
|
|
|
|
final url = Uri.parse(videoUrl);
|
|
|
|
controller = VideoPlayerController.networkUrl(
|
|
|
|
url,
|
2024-06-26 21:31:55 +02:00
|
|
|
httpHeaders: ApiService.getRequestHeaders(),
|
2024-06-11 18:14:49 +02:00
|
|
|
videoPlayerOptions: asset.livePhotoVideoId != null
|
|
|
|
? VideoPlayerOptions(mixWithOthers: true)
|
|
|
|
: VideoPlayerOptions(mixWithOthers: false),
|
2024-03-06 04:42:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await controller.initialize();
|
|
|
|
|
|
|
|
ref.onDispose(() {
|
|
|
|
controller.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
return controller;
|
|
|
|
}
|