From c50cdf68b4dfd863945169232a02a74bd9ee0ddd Mon Sep 17 00:00:00 2001 From: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Date: Wed, 11 Sep 2024 02:03:14 +0530 Subject: [PATCH] fix: aspect ratio --- .../common/native_video_viewer.page.dart | 35 ++++++++++++------- mobile/pubspec.lock | 4 +-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/mobile/lib/pages/common/native_video_viewer.page.dart b/mobile/lib/pages/common/native_video_viewer.page.dart index e0cbcd0f09daf..df1b03f21825e 100644 --- a/mobile/lib/pages/common/native_video_viewer.page.dart +++ b/mobile/lib/pages/common/native_video_viewer.page.dart @@ -37,6 +37,8 @@ class NativeVideoViewerPage extends HookConsumerWidget { final controller = useState(null); final lastVideoPosition = useRef(-1); final isBuffering = useRef(false); + final width = useRef(asset.width?.toDouble() ?? 1.0); + final height = useRef(asset.height?.toDouble() ?? 1.0); void checkIfBuffering([Timer? timer]) { if (!context.mounted) { @@ -60,10 +62,15 @@ class NativeVideoViewerPage extends HookConsumerWidget { Future createSource(Asset asset) async { if (asset.isLocal && asset.livePhotoVideoId == null) { - final file = await asset.local!.file; - if (file == null) { + final entity = await asset.local!.obtainForNewProperties(); + final file = await entity?.file; + if (entity == null || file == null) { throw Exception('No file found for the video'); } + + width.value = entity.orientatedWidth.toDouble(); + height.value = entity.orientatedHeight.toDouble(); + return await VideoSource.init( path: file.path, type: VideoSourceType.file, @@ -165,18 +172,15 @@ class NativeVideoViewerPage extends HookConsumerWidget { return; } - controller.value = nc; - - controller.value?.onPlaybackPositionChanged - .addListener(onPlaybackPositionChanged); - controller.value?.onPlaybackStatusChanged - .addListener(onPlaybackPositionChanged); - controller.value?.onPlaybackReady.addListener(onPlaybackReady); - controller.value?.onPlaybackEnded.addListener(onPlaybackEnded); + nc.onPlaybackPositionChanged.addListener(onPlaybackPositionChanged); + nc.onPlaybackStatusChanged.addListener(onPlaybackPositionChanged); + nc.onPlaybackReady.addListener(onPlaybackReady); + nc.onPlaybackEnded.addListener(onPlaybackEnded); final videoSource = await createSource(asset); - controller.value?.loadVideoSource(videoSource); + nc.loadVideoSource(videoSource); + controller.value = nc; Timer(const Duration(milliseconds: 200), checkIfBuffering); } @@ -206,6 +210,13 @@ class NativeVideoViewerPage extends HookConsumerWidget { [], ); + double calculateAspectRatio() { + if (width.value == 0 || height.value == 0) { + return 1; + } + return width.value / height.value; + } + final size = MediaQuery.sizeOf(context); return SizedBox( @@ -224,7 +235,7 @@ class NativeVideoViewerPage extends HookConsumerWidget { children: [ Center( child: AspectRatio( - aspectRatio: (asset.width ?? 1) / (asset.height ?? 1), + aspectRatio: calculateAspectRatio(), child: NativeVideoPlayerView( onViewReady: initController, ), diff --git a/mobile/pubspec.lock b/mobile/pubspec.lock index 3a9c895cef7d6..e6b18bb1f8df9 100644 --- a/mobile/pubspec.lock +++ b/mobile/pubspec.lock @@ -1746,10 +1746,10 @@ packages: dependency: transitive description: name: vm_service - sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.2.4" + version: "14.2.5" wakelock_plus: dependency: "direct main" description: