2012-06-18 9 views
14

Mam aplikację, która eksportuje plik AVMutableComposition do pliku . Chciałbym, aby użytkownik zobaczył status eksportu z paskiem postępu w taki sam sposób, jak w przypadku wysłania wiadomość tekstową lub załadowany plik.Pasek postępu dla AVAssetExportSession

W jaki sposób utworzyć pasek postępu, gdy wiem, czas trwania zadania (np. Odtwarzanie pliku audio), ale ponieważ nie ma ustalonego czasu trwania eksportu, nie jestem pewien, jak postępować.

Mam obecnie wskaźnik aktywności, ale nie zapewnia on optymalnego komfortu użytkowania.

Czy ktoś ma jakieś wskazówki?

Odpowiedz

29

wymyśliłem odpowiedź jakiś czas temu więc wyślę go w przypadku może pomóc komuś:

Po pierwsze, w sposobie, w której dzwonisz AVAssetExportSession masz skonfigurować timer zaktualizować UIProgressView gdy już zainicjowane wywozu:

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

to trzeba metodę zaktualizować wyświetlacz, biorąc pod uwagę, że nieruchomość na AVAssetExportSession postęp idzie od 0 - 1:

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

dzwonisz 'self.exportProgressBarTimer =' wewnątrz lub na zewnątrz bloku exportAsynchronouslyWithCompletionHandler' '? 'self.exportSession.progress' zawsze wyświetla się dla mnie jako 1.0 w' updateExportDisplay'. –

+0

Poza blokiem 'exportAsynchronouslyWithCompletionHandler'. Działa to dla mnie pięknie. –

2

sam problem mam do czynienia w iOS 8.0, postanowiłem go za pomocą wysyłki 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

użycia poniżej linii kodu.

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; 
     }); 
    } 
}); 

referencyjny Link: https://github.com/keijiro/iOS4BookSampleCode/blob/master/3.3.SimpleExport/Classes/SimpleExportViewController.m