Browse Source

上传逻辑优化

Steven 4 years ago
parent
commit
31dd577d4b

+ 35 - 9
MusicGradeExam/MusicGradeExam/UI/Home/SimulationExam/Controller/SimulationExamRecordController.m

@@ -58,7 +58,6 @@
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
-    
 }
 
 - (void)viewDidDisappear:(BOOL)animated {
@@ -183,7 +182,10 @@
         recordCtrl.songMessageArray = songNameArray;
         recordCtrl.singleSongRecordMinutes = self.sourceModel.singleSongRecordMinutes;
         [recordCtrl configTime:self.topView.duration recordFinishBlock:^(VIDEORECORDACTION action, NSMutableArray *videoArray) {
-            [self refreshAllWithVideoArray:videoArray songArray:songNameArray];
+            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+                [self refreshAllWithVideoArray:videoArray songArray:songNameArray];
+            });
+            
         }];
         recordCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
         [self presentViewController:recordCtrl animated:YES completion:nil];
@@ -198,26 +200,50 @@
     dispatch_main_async_safe(^{
         [MBProgressHUD ksShowHUDWithText:@"视频处理中..."];
     });
-    MJWeakSelf;
-    for (NSInteger index = 0 ;index < videoArray.count; index++) {
+    
+    [self outputVideoFileIndex:0 videoArray:videoArray songArray:songArray];
+}
+
+- (void)outputVideoFileIndex:(NSInteger)index videoArray:(NSArray *)videoArray songArray:(NSArray *)songArray  {
+    __block NSInteger fileIndex = index;
+    if (videoArray.count && videoArray.count > index) {
         PHAsset *assset = videoArray[index];
+        MJWeakSelf;
         [[TZImageManager manager] getVideoOutputPathWithAsset:assset presetName:AVAssetExportPresetMediumQuality success:^(NSString *outputPath) {
             NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
             NSData *outputData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:outputPath]]; //压缩后的视频
             NSLog(@"导出后的视频:%@",[NSString stringWithFormat:@"%.2fM",(CGFloat)outputData.length/(1024*1024)]);
-            NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, songArray[index]];
-            [weakSelf refreshView:outputPath fileKey:fileKey];
-            [MBProgressHUD ksHideHUD];
+            NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, songArray[fileIndex]];
+            dispatch_main_async_safe(^{
+                fileIndex++;
+                [weakSelf refreshView:outputPath fileKey:fileKey];
+                if (fileIndex == videoArray.count) {
+                    [MBProgressHUD ksHideHUD];
+                }
+                else {
+                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+                        [weakSelf outputVideoFileIndex:fileIndex videoArray:videoArray songArray:songArray];
+                    });
+                }
+            });
         } failure:^(NSString *errorMessage, NSError *error) {
             dispatch_main_async_safe(^{
                 [MBProgressHUD ksShowMessage:@"视频导出失败"];
-                [MBProgressHUD ksHideHUD];
+                fileIndex++;
+                if (fileIndex == videoArray.count) {
+                    [MBProgressHUD ksHideHUD];
+                }
+                else {
+                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+                        [self outputVideoFileIndex:fileIndex videoArray:videoArray songArray:songArray];
+                    });
+                }
             });
         }];
+        
     }
 }
 
-
 - (void)refreshView:(NSString *)videoUrl fileKey:(NSString *)fileKey {
     NSString *fileUrl = videoUrl;
     NSString *fileName = [[videoUrl componentsSeparatedByString:@"/"] lastObject];

+ 7 - 5
MusicGradeExam/MusicGradeExam/UI/RecordAction/KSVideoRecordViewController.m

@@ -381,10 +381,12 @@
     // 是否保存到相册
     [[TZImageManager manager] saveVideoWithUrl:outputFileURL completion:^(PHAsset *asset, NSError *error) {
         self.isRecording = NO;
-        // 删除文件
-        [self removeVideoWithPath:outputFileURL.path];
-        // 记录当前相册asset
-        [self.videoAssetArray addObject:asset];
+        if (!error) {
+            // 删除文件
+            [self removeVideoWithPath:outputFileURL.path];
+            // 记录当前相册asset
+            [self.videoAssetArray addObject:asset];
+        }
         if (self.isQuiting) { // 保存完成退出
             [MBProgressHUD ksHideHUD];
             self.callback(VIDEORECORDACTION_BACK, self.videoAssetArray);
@@ -566,7 +568,7 @@
             NSString *songName = [[self.songMessageArray objectAtIndex:self.currentRecordIndex] MD5];
             NSString *fileName = [NSString stringWithFormat:@"%@.mp4",songName];
             NSString *filePath = [path stringByAppendingPathComponent:fileName];
-                    NSLog(@"%@",filePath);
+                    NSLog(@"--------------%@",filePath);
                     [self.captureMovieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];
         }
     }

+ 33 - 62
MusicGradeExam/MusicGradeExam/UI/RecordExam/Controller/RecordExamViewController.m

@@ -204,7 +204,10 @@
         recordCtrl.songMessageArray = songNameArray;
         recordCtrl.singleSongRecordMinutes = self.sourceModel.singleSongRecordMinutes;
         [recordCtrl configTime:self.topView.duration recordFinishBlock:^(VIDEORECORDACTION action, NSMutableArray *videoArray) {
-            [self uploadVideoWithVideoArray:videoArray songArray:songNameArray];
+            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+                [self uploadVideoWithVideoArray:videoArray songArray:songNameArray];
+            });
+            
         }];
         recordCtrl.modalPresentationStyle = UIModalPresentationFullScreen;
         [self presentViewController:recordCtrl animated:YES completion:nil];
@@ -216,37 +219,47 @@
 }
 
 - (void)uploadVideoWithVideoArray:(NSMutableArray *)videoArray songArray:(NSArray *)songArray {
-    dispatch_main_async_safe(^{
-        [MBProgressHUD ksShowHUDWithText:@"视频处理中..."];
-    });
-    NSMutableArray *urlArray = [NSMutableArray array];
-    NSMutableArray *fileKeyArray = [NSMutableArray array];
-    MJWeakSelf;
-    for (NSInteger index = 0 ;index < videoArray.count; index++) {
+    [self outputVideoFileIndex:0 videoArray:videoArray songArray:songArray];
+}
+
+- (void)outputVideoFileIndex:(NSInteger)index videoArray:(NSArray *)videoArray songArray:(NSArray *)songArray {
+    __block NSInteger fileIndex = index;
+    if (videoArray.count && videoArray.count > index) {
+        dispatch_main_async_safe(^{
+            [MBProgressHUD ksShowHUDWithText:@"视频处理中..."];
+        });
         PHAsset *assset = videoArray[index];
+        MJWeakSelf;
         [[TZImageManager manager] getVideoOutputPathWithAsset:assset presetName:AVAssetExportPresetMediumQuality success:^(NSString *outputPath) {
             NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
             NSData *outputData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:outputPath]]; //压缩后的视频
             NSLog(@"导出后的视频:%@",[NSString stringWithFormat:@"%.2fM",(CGFloat)outputData.length/(1024*1024)]);
-            NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, songArray[index]];
-            [urlArray addObject:outputPath];
-            [fileKeyArray addObject:fileKey];
-            if (index == videoArray.count - 1) {
+            NSString *fileKey = [NSString stringWithFormat:@"%@%@", self.examRegistrationId, songArray[fileIndex]];
+            dispatch_main_async_safe(^{
                 [MBProgressHUD ksHideHUD];
-                // 批量上传
-                [weakSelf mutilUploadVideoWithUrl:urlArray fileKeyArray:fileKeyArray];
-            }
-            
+                fileIndex++;
+                // 上传
+                [weakSelf uploadVideoWithUrl:outputPath fileKey:fileKey videoArray:videoArray songArray:songArray fileIndex:fileIndex];
+            });
         } failure:^(NSString *errorMessage, NSError *error) {
             dispatch_main_async_safe(^{
                 [MBProgressHUD ksShowMessage:@"视频导出失败"];
-                [MBProgressHUD ksHideHUD];
+                fileIndex++;
+                if (fileIndex == videoArray.count) {
+                    [MBProgressHUD ksHideHUD];
+                }
+                else {
+                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+                        // 继续导出
+                        [self outputVideoFileIndex:fileIndex videoArray:videoArray songArray:songArray];
+                    });
+                }
             });
         }];
     }
-    
 }
 
+
 - (void)deleteFileWithKey:(NSString *)fileKey {
     NSDictionary *parm = UserDefault(fileKey);
     NSString *localUrl = [parm stringValueForKey:@"localFileUrl"];
@@ -408,49 +421,7 @@
 }
 
 #pragma mark ------ 上传视频文件
-- (void)mutilUploadVideoWithUrl:(NSMutableArray *)videoUrlArray fileKeyArray:(NSMutableArray *)fileKeyArray {
-    [self hudTipWillShow:YES];
-    NSMutableArray *fileDataArray = [NSMutableArray array];
-    for (NSString *videoUrl in videoUrlArray) {
-        [fileDataArray addObject:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoUrl]]];
-    }
-    [KSRequestManager mutiVideoFileUpload:KS_POST fileDataArray:fileDataArray progress:^(int64_t bytesWritten, int64_t totalBytes) {
-        dispatch_main_async_safe(^{
-            // 显示进度
-            if (self.HUD) {
-                self.HUD.progress = bytesWritten / totalBytes;// progress是回调进度
-            }
-        });
-        
-    } success:^(NSArray * _Nonnull dics) {
-        [self hudTipWillShow:NO];
-        for (NSInteger index = 0; index < dics.count; index++) {
-            NSDictionary *dic = [dics objectAtIndex:index];
-            if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
-                NSString *fileUrl = [[dic dictionaryValueForKey:@"data"] stringValueForKey:@"url"];
-                NSString *fileName = [[fileUrl componentsSeparatedByString:@"/"] lastObject];
-                NSString *videoUrl = [fileDataArray objectAtIndex:index];
-                NSString *fileKey = [fileKeyArray objectAtIndex:index];
-                // 保存文件路径
-                NSDictionary *parm = @{@"localFileUrl":videoUrl,
-                                       @"remoteUrl":fileUrl,
-                                       @"fileName": fileName};
-                UserDefaultSet(parm, fileKey);
-                [self.fileUrlArray addObject:fileUrl];
-                [self checkSubmitButtonEnable];
-            }
-            else {
-                [self MBPShow:MESSAGEKEY];
-            }
-        }
-        [self.tableView reloadData];
-        
-    } faliure:^(NSError * _Nonnull error) {
-        [self hudTipWillShow:NO];
-    }];
-}
-
-- (void)uploadVideoWithUrl:(NSString *)videoUrl fileKey:(NSString *)fileKey {
+- (void)uploadVideoWithUrl:(NSString *)videoUrl fileKey:(NSString *)fileKey videoArray:(NSArray *)videoArray songArray:(NSArray *)songArray fileIndex:(NSInteger)fileIndex {
     [self hudTipWillShow:YES];
     
     NSData *fileData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoUrl]];
@@ -478,8 +449,8 @@
         }
         else {
             [self MBPShow:MESSAGEKEY];
-            
         }
+        [self outputVideoFileIndex:fileIndex videoArray:videoArray songArray:songArray];
     } faliure:^(NSError * _Nonnull error) {
         [self hudTipWillShow:NO];
     }];