Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVURLAsset URLAssetWithURL:options: blocks main thread with remote URL?

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.

like image 877
tettoffensive Avatar asked Oct 29 '25 10:10

tettoffensive


1 Answers

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)
})
like image 91
Ed Wellbrook Avatar answered Oct 31 '25 01:10

Ed Wellbrook



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!