Browse Source

分离getFilesize重复引用

gavin.chen 2 tháng trước cách đây
mục cha
commit
7b9b6c6b48
1 tập tin đã thay đổi với 34 bổ sung10 xóa
  1. 34 10
      lib/async_vid/shell/reader.dart

+ 34 - 10
lib/async_vid/shell/reader.dart

@@ -34,13 +34,25 @@ class AsyncShellVidDataReader extends AsyncVidDataReaderBase {
 
   @override
   void startDownload() async {
+    if (isVrd) {
+      final imageCount = await getImageCount();
+      if (imageCount == 0) {
+        setError(true, 'Get image count is 0.');
+        return;
+      }
+      await fetchFrames(0, 1, isNeedReload: true);
+      if (imageCount == null) {
+        setError(true, 'Get image count fail.');
+        return;
+      }
+      _imageCount = imageCount;
+      fetchFrames(0, _imageCount, isNeedReload: true);
+      return;
+    }
     final size = await getFileSize();
     // 获取到size即开始下载
     if (size == null) {
       setError(true, 'Get file size fail.');
-    } else if (isVrd) {
-      _imageCount = size;
-      fetchFrames(0, size, isNeedReload: true);
     }
   }
 
@@ -76,10 +88,9 @@ class AsyncShellVidDataReader extends AsyncVidDataReaderBase {
     updateProgress(progress);
   }
 
-  //注意:此处VRD返回的是总帧数,非VRD返回的是数据大小
   Future<int?> getFileSize() async {
     final result = await ShellApiHelper.call(
-      (isVrd) ? 'dynamicFetchVidAsync' : 'fetchVid',
+      'fetchVid',
       {
         'Id': id,
         'Url': url,
@@ -87,13 +98,26 @@ class AsyncShellVidDataReader extends AsyncVidDataReaderBase {
       },
     );
     if (result != null) {
-      if (isVrd) {
-        _imageCount = result!;
-      }
+      initChunk(result!);
+    }
+    return result;
+  }
+
+  // 此处返回 VRD 的总帧数
+  Future<int?> getImageCount() async {
+    final result = await ShellApiHelper.call(
+      'dynamicFetchVidAsync',
+      {
+        'Id': id,
+        'Url': url,
+        'MinChunkSize': minChunkSize,
+      },
+    );
+    if (result != null) {
+      _imageCount = result!;
       initChunk(result);
     }
-    await fetchFrames(0, 1, isNeedReload: true);
-    print("getFileSize result:$result");
+    print("getImageCount result:$result");
     return result;
   }