2012-06-18 42 views
14

Tôi đã có ứng dụng xuất AVMutableComposition thành tệp .mov và tôi muốn người dùng thấy trạng thái xuất bằng thanh tiến trình giống như cách bạn gửi nếu bạn gửi một tin nhắn văn bản hoặc tải lên một tập tin.Thanh tiến trình cho AVAssetExportSession

Tôi biết cách tạo thanh tiến trình khi tôi biết thời lượng của tác vụ (chẳng hạn như phát tệp âm thanh), nhưng vì không có thời lượng đã đặt cho xuất nên tôi không chắc chắn cách tiếp tục.

Tôi hiện đã có chỉ báo hoạt động nhưng không cung cấp trải nghiệm người dùng tốt nhất.

Có ai có bất kỳ con trỏ nào không?

Trả lời

29

tôi đã đưa ra một câu trả lời một khi trở lại vì vậy tôi sẽ đăng nó trong trường hợp nó có thể giúp ai đó:

Thứ nhất, trong phương pháp mà bạn gọi AVAssetExportSession bạn đã có để thiết lập một bộ đếm thời gian để cập nhật UIProgressView của bạn một khi bạn đã bắt đầu xuất khẩu:

//`AVAssetExportSession` code here 
    self.exportProgressBarTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateExportDisplay) userInfo:nil repeats:YES]; 
... 

Sau đó, bạn cần một phương pháp để cập nhật hiển thị của bạn có tính đến tài khoản đó là tài sản tiến bộ về AVAssetExportSession đi 0-1:

- (void)updateExportDisplay { 
    self.exportProgressBar.progress = exportSession.progress; 
    if (self.exportProgressBar.progress > .99) { 
     [self.exportProgressBarTimer invalidate]; 
    } 
} 
+0

quí vị muốn gọi 'self.exportProgressBarTimer = 'bên trong hoặc bên ngoài của' exportAsynchronouslyWithCompletionHandler' khối? 'self.exportSession.progress' luôn hiển thị là 1.0 trong' updateExportDisplay' cho tôi. –

+0

Bên ngoài khối 'exportAsynchronouslyWithCompletionHandler'. Nó làm việc rất đẹp cho tôi. –

2

Cùng một vấn đề tôi đã phải đối mặt trong iOS 8.0, tôi giải quyết nó bằng cách sử văn quee

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler{ 

[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; 

exportSession2 = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; 
exportSession2.outputURL = outputURL; 
exportSession2.outputFileType = AVFileTypeQuickTimeMovie; 

[exportSession2 exportAsynchronouslyWithCompletionHandler:^(void) 
{ 
    handler(exportSession2); 
}]; 

dispatch_async(dispatch_get_main_queue(), ^(void){ 

     self.exportProgressBarTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateExportDisplay) userInfo:nil repeats:YES]; 
}); 

}

1

Sử dụng dưới dòng mã.

AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetMediumQuality]; 
self.exportSession = session; 

// 出力先(テンポラリファイル)の設定。 
NSString *filePath = NSTemporaryDirectory(); 
filePath = [filePath stringByAppendingPathComponent:@"out.mov"]; 
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil]; 
session.outputURL = [NSURL fileURLWithPath:filePath]; 

// 出力タイプの設定。 
session.outputFileType = AVFileTypeQuickTimeMovie; 

// 非同期エクスポートの開始。 
[session exportAsynchronouslyWithCompletionHandler:^{ 
    if (session.status == AVAssetExportSessionStatusCompleted) { 
     // フォトアルバムへの書き込み。 
     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
     [library writeVideoAtPathToSavedPhotosAlbum:session.outputURL completionBlock:^(NSURL *assetURL, NSError *error){ 
      if (error) { 
       self.resultLabel.text = [NSString stringWithFormat:@"アセット書き込み失敗\n%@", error]; 
      } else { 
       self.resultLabel.text = [NSString stringWithFormat:@"完了\n%@", assetURL]; 
      } 
     }]; 
     [library autorelease]; 
    } else if (session.status == AVAssetExportSessionStatusCancelled) { 
     self.resultLabel.text = @"エクスポート中断"; 
    } else { 
     self.resultLabel.text = [NSString stringWithFormat:@"エクスポート失敗\n%@", session.error]; 
    } 
}]; 


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
    while (session.status == AVAssetExportSessionStatusExporting) { 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      self.progressView.progress = session.progress; 
     }); 
    } 
}); 

tham khảo Link: https://github.com/keijiro/iOS4BookSampleCode/blob/master/3.3.SimpleExport/Classes/SimpleExportViewController.m

Các vấn đề liên quan