Search code examples
swiftkey-value-observingavassetexportsession

AVAssetExportSession status kvo set up


I'm working with AVAssetExportSession. I'm not sure why the result is not getting a video showing up so I want to observe the status of the AVAssetExportSession. It is my first time doing the KVO.

AVAssetExportSession.Status

it mentions that the AVAssetExportSession.Status is observable. I set it by

        guard let export = AVAssetExportSession(
          asset: composition,
          presetName: AVAssetExportPresetHighestQuality)
          else {
            print("Cannot create export session.")
            fatalError()
        }
        export.observe(\AVAssetExportSession.status, options: .new) {
            export, change in
            print("export.status : \(export.status)")
        }

I executed the export session and when I check status in the async block, it says completed. I would like to see the progress of the status from starting to completed so I deleted this code to print status.

export.videoComposition = videoComposition
        export.outputFileType = .mov
        export.outputURL = exportURL
        
        export.exportAsynchronously {
          DispatchQueue.main.async {

          }
        }

I set the breakpoint in kvo block but it seems it's not coming in anything.


Solution

  • You need to hold a refrence to it

    var keyVO:NSKeyValueObservation!
    func viewDidLoad() {
       super.viewDidLoad() 
       keyVO = export.observe(\AVAssetExportSession.status, options: .new) {  export, change in
          print("export.status : \(export.status)")
       }
    }