I\'ve got an app that exports an AVMutableComposition
into a .mov
file, and I\'d like for the user to see the status of the export with a progress bar
Swift 3 example
Using Notification Center to send progress updates to listeners
//`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]
}
})
Then setup the notification center listener anywhere you'd like using
NotificationCenter.default.addObserver(self, selector: #selector(self.statusUpdate(_:)), name: NSNotification.Name(rawValue: "ProgressBarPercentage"), object: nil)