Calculate download bandwidth for AVAggregateAssetDownloadTask

Hi all,

I was wondering if it is possible to calculate the bandwidth of a download in progress? I am using AVAggregateAssetDownloadTask From Apple's sample code I know the best way to show "download progress" is by doing something like this:

   func urlSession(_ session: URLSession, aggregateAssetDownloadTask: AVAggregateAssetDownloadTask,
          didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue],
          timeRangeExpectedToLoad: CMTimeRange, for mediaSelection: AVMediaSelection) {
    ...
    var percentComplete = 0.0
    for value in loadedTimeRanges {
      let loadedTimeRange: CMTimeRange = value.timeRangeValue
      percentComplete +=
        loadedTimeRange.duration.seconds / timeRangeExpectedToLoad.duration.seconds
   ...
   // The following values are wrong when comparing actual size of files on disk.
   let fetchedBytes = counter.string(fromByteCount: aggregateAssetDownloadTask.countOfBytesReceived)
    let expectedBytes = counter.string(fromByteCount: aggregateAssetDownloadTask.countOfBytesExpectedToReceive)
    }
}

AVAggregateAssetDownloadTask also has URLSessionTasks field countOfBytesReceived and countOfBytesExpectedToReceive

Trouble is if I try to read either of these two, as per the code above, the count is incorrect. I.e. if I check the size of the movpkg on disk, it is quite off from what countOfBytesReceived states.

My questions are:

  1. Is a time range calculation still the best way to depict download progress
  2. Is there a a way to get correct updates of the movpkg size as the download progresses, so that I can calculate speed?

Hello! Probably you've found any solution of your question? I'm in same situation for now.

Calculate download bandwidth for AVAggregateAssetDownloadTask
 
 
Q