2012-06-18 22 views
14

AVMutableComposition.movファイルにエクスポートするアプリがあります。送信した場合と同じように、プログレスバーを使ってエクスポートのステータスを表示することができますテキストメッセージ、またはファイルをアップロードしました。AVAssetExportSessionのプログレスバー

私はタスクの期間(オーディオファイルの再生など)を知っているときに進捗バーを作成する方法を知っていますが、エクスポートの期間が設定されていないため、進める方法がわかりません。

現在のところ、アクティビティインジケータがありますが、これはユーザーエクスペリエンスが最高です。

誰でもポインタを持っていますか?あなたがするタイマーを設定するんだAVAssetExportSession呼び出すする方法では、

まず:

答えて

29

私はので、私はそれが誰かを助けることができる場合には、それを投稿しますしばらく前に答えを思い付きましたあなたのUIProgressViewは更新エクスポートを開始したら:

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

次にあなたがAVAssetExportSessionの進捗プロパティは0から行くことを考慮に入れて、あなたの表示を更新する方法を必要とする - 1:

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

あなたはself.exportProgressBarTimer = ''内部またはexportAsynchronouslyWithCompletionHandler'ブロックの外側で '呼んでいますか? 'self.exportSession.progress'は私にとって' updateExportDisplay'には常に1.0として表示されます。 –

+0

'exportAsynchronouslyWithCompletionHandler'ブロックの外側です。それは私にとって美しく働いています。 –

2

私はiOSの8.0に直面している同じ問題は、私は派遣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

コードの行の下に使用を使用して、それを解決しました。

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

参考リンク:https://github.com/keijiro/iOS4BookSampleCode/blob/master/3.3.SimpleExport/Classes/SimpleExportViewController.m

関連する問題