AVAssetExportSession的进度条

Orp*_*ury 18 iphone cocoa-touch uiprogressview avassetexportsession

我有一个将应用程序导出AVMutableComposition.mov文件中的应用程序,我希望用户使用进度条查看导出状态的方式与发送文本消息或上传文件时的方式相同.

我知道如何在知道任务的持续时间(例如播放音频文件)时创建进度条,但由于导出没有设定的持续时间,我不确定如何继续.

我目前有一个活动指示器,但它没有提供最佳的用户体验.

有没有人有任何指针?

Orp*_*ury 35

我不久前想出了一个答案所以我会发布它,以防它可以帮助某人:

首先,在您调用的方法中,您AVAssetExportSession必须设置一个计时器,以便在UIProgressView您启动导出后更新您的计时器:

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

然后你需要一个方法来更新你的显示,考虑到progress属性AVAssetExportSession从0到1:

- (void)updateExportDisplay {
    self.exportProgressBar.progress = exportSession.progress;
    if (self.exportProgressBar.progress > .99) {
        [self.exportProgressBarTimer invalidate];
    }
}
Run Code Online (Sandbox Code Playgroud)


and*_*yjr 8

斯威夫特 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]
   }
})
Run Code Online (Sandbox Code Playgroud)

然后在您想要使用的任何地方设置通知中心侦听器

NotificationCenter.default.addObserver(self, selector: #selector(self.statusUpdate(_:)), name: NSNotification.Name(rawValue: "ProgressBarPercentage"), object: nil)
Run Code Online (Sandbox Code Playgroud)