Progress bar for AVAssetExportSession

后端 未结 4 601
遇见更好的自我
遇见更好的自我 2021-02-02 11:57

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

4条回答
  •  清酒与你
    2021-02-02 12:56

    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)
    

提交回复
热议问题