2012-06-18 5 views
14

Ho un'app che esporta un AVMutableComposition in un file .mov e mi piacerebbe che l'utente visualizzasse lo stato dell'esportazione con una barra di avanzamento nello stesso modo in cui verrebbe inviato un messaggio di testo o caricato un file.Barra di avanzamento per AVAssetExportSession

So come creare una barra di avanzamento quando conosco la durata di un'attività (ad esempio la riproduzione di un file audio), ma poiché non c'è una durata impostata per l'esportazione non sono sicuro su come procedere.

Attualmente ho un indicatore di attività ma non fornisce la migliore esperienza utente.

Qualcuno ha qualche indicazione?

risposta

29

mi si avvicinò con una risposta un po 'indietro in modo vi posterò in caso può aiutare qualcuno:

In primo luogo, nel metodo in cui si chiama AVAssetExportSession hai avuto modo di impostare un timer per aggiornare il UIProgressView una volta avviata l'esportazione:

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

allora avete bisogno di un metodo per aggiornare il display tenendo conto che la proprietà progressi AVAssetExportSession va 0-1:

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

stai chiamando 'self.exportProgressBarTimer =' all'interno o all'esterno del blocco 'exportAsynchronouslyWithCompletionHandler'? 'self.exportSession.progress' mostra sempre come 1.0 in' updateExportDisplay' per me. –

+0

Al di fuori del blocco 'exportAsynchronouslyWithCompletionHandler'. Funziona magnificamente per me. –

2

Stesso problema che ho affrontato in iOS 8.0, ho risolto utilizzando la spedizione 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

Usa al di sotto di linee di codice.

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

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