我有一个出口的一个应用程序AVMutableComposition
成.mov
文件,我想为用户看到一个进步的出口的状态栏,你会如果你发来的短信或上传了同样的方式文件。
我知道如何创建一个进度条,当我知道一个任务的持续时间(如播放音频文件),但因为有针对出口我不确定如何进行没有固定的时间。
我有一个活动的指标,但目前它不提供最佳的用户体验。
有没有人有任何指针?
我有一个出口的一个应用程序AVMutableComposition
成.mov
文件,我想为用户看到一个进步的出口的状态栏,你会如果你发来的短信或上传了同样的方式文件。
我知道如何创建一个进度条,当我知道一个任务的持续时间(如播放音频文件),但因为有针对出口我不确定如何进行没有固定的时间。
我有一个活动的指标,但目前它不提供最佳的用户体验。
有没有人有任何指针?
我想出了一个答案而回,所以我会在情况下,它可以帮助别人张贴:
首先,在你调用方法AVAssetExportSession
你要建立一个定时器来更新您的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];
}
}
我所面临的的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];
});
}
迅速3示例
使用通知中心发送进度更新听众
//`AVAssetExportSession` code above
var exportProgressBarTimer = Timer() // initialize timer
if #available(iOS 10.0, *) {
exportProgressBarTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
// Get Progress
let progress = Float((exportSession?.progress)!);
if (progress < 0.99) {
let dict:[String: Float] = ["progress": progress]
NotificationCenter.default.post(name: Notification.Name("ProgressBarPercentage"), object: nil, userInfo: dict)
}
}
}
// on exportSession completed
exportSession?.exportAsynchronously(completionHandler: {
exportProgressBarTimer.invalidate(); // remove/invalidate timer
if exportSession?.status == AVAssetExportSessionStatus.completed {
// [....Some Completion Code Here]
}
})
然后设置通知中心监听任何你想使用
NotificationCenter.default.addObserver(self, selector: #selector(self.statusUpdate(_:)), name: NSNotification.Name(rawValue: "ProgressBarPercentage"), object: nil)
使用下面的代码行。
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