4

I've found no documentation about this, but in my practical experience

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

(where url is a remote URL for an HLS live stream), will block the main thread if the network is down or for some reason the file cannot be read.

Has anyone else noticed this? I may end up changing my setup to use GCD to construct on a background thread. Because as it is the UI locks up any time videos can't be loaded.

The AVPlayerItem loads things asynchronously, but AVURLAsset does not seem to do this.

tettoffensive
  • 664
  • 7
  • 24

1 Answers1

3

Had this issue too. Solved it with the following:

let asset = AVURLAsset(url: url)

// I'm using a resource loader for my custom urls...
let loaderQueue = DispatchQueue(label: "loader-queue", qos: .userInteractive)
asset.resourceLoader.setDelegate(self, queue: self.loaderQueue)

// load values asynchronously and once complete, create the player item
let keys = ["duration", "tracks"]
asset.loadValuesAsynchronously(forKeys: keys, completionHandler: {
    let item = AVPlayerItem(asset: asset)
    self.player.insert(item, after: nil)
})
Ed Wellbrook
  • 58
  • 1
  • 5