I’m utilizing AVQueuePlayer to play the objects in a queue in my app. So when there are suppose 10 objects so as to add to queue, I merely use a for loop so as to add them to queue. More often than not it really works wonderful however generally it freezes the entire app.
Thread at all times hangs right here let asset = AVURLAsset(url: url), then freeze
Right here is my code –
func loadAssets() {
let URLS = ["http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4","http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4","http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4","http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4"]
for urlString in URLS {
let contentURL = URL(string: urlString)!
self.createAVAsset(url: contentUrl, completion: ({ [weak self] asset in
guard let self = self else { return }
let playerItem = self.createPlayerItemForAsset(asset)
self.queuePlayer?.insert(playerItem, after: nil)
}))
}
}
func createAVAsset(url: URL, completion: @escaping ((AVURLAsset) -> Void)) {
let asset = AVURLAsset(url: url) /*THREAD HANGS ON THIS LINE*/
asset.resourceLoader.setDelegate(self, queue: self.queue)
completion(asset)
}
I’ve tried many various issues within the methodology createAVAsset however nothing occurred additionally this occurs unexpectedly not reproducible every time.
Varients I attempted to repair the problem are –
- Utilizing background thread – Not working correctly
func createAVAsset(url: URL, completion: @escaping ((AVURLAsset) -> Void)) {
DispatchQueue.world(qos: .background).async { [weak self] in
guard let self = self else { return }
let asset = AVURLAsset(url: url) /*THREAD HANGS ON THIS LINE*/
asset.resourceLoader.setDelegate(self, queue: self.queue)
DispatchQueue.foremost.async {
completion(asset)
}
}
}
- Utilizing loadValuesAsynchronously – Not working correctly
func createAVAsset(url: URL, completion: @escaping ((AVURLAsset) -> Void)) {
let asset = AVURLAsset(url: url) /*THREAD HANGS ON THIS LINE*/
let keys: [String] = ["playable"]
//Management does not attain beneath, it hangs on above line already
asset.loadValuesAsynchronously(forKeys: keys, completionHandler: {
var error: NSError? = nil
let standing = asset.statusOfValue(forKey: "playable", error: &error)
swap standing {
case .loaded:
DispatchQueue.foremost.async {
//do one thing, present alert, put a placeholder picture and so on.
completion(asset)
}
break
case .failed:
DispatchQueue.foremost.async {
//do one thing, present alert, put a placeholder picture and so on.
}
break
case .cancelled:
DispatchQueue.foremost.async {
//do one thing, present alert, put a placeholder picture and so on.
}
break
default:
break
}
})
}