AVURLAsset URLAssetWithURL:options:使用远程URL阻止主线程?

时间:2016-08-04 21:03:51

标签: ios multithreading grand-central-dispatch avurlasset

我没有找到关于此的文档,但根据我的实践经验

        var statusCode: OSStatus
        var publicKey: SecKey?
        var privateKey: SecKey?

        let publicKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true, kSecAttrApplicationTag:"publicTag".dataUsingEncoding(NSUTF8StringEncoding)!]
        let privateKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true, kSecAttrApplicationTag:"privateTag".dataUsingEncoding(NSUTF8StringEncoding)!]

        var keyPairAttr = [NSObject: NSObject]()
        keyPairAttr[kSecAttrKeyType] = kSecAttrKeyTypeRSA
        keyPairAttr[kSecAttrKeySizeInBits] = 512
        keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttr
        keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAttr

        statusCode = SecKeyGeneratePair(keyPairAttr, &publicKey, &privateKey)

        if statusCode == noErr && publicKey != nil && privateKey != nil {
            printMessage = "Key pair generated OK"
            print("Public Key: \(publicKey!)")
            print("Private Key: \(privateKey!)")
        } else {
            printMessage = "Error generating key pair: \(statusCode)"
        }

(其中url是HLS实时流的远程URL),如果网络关闭或由于某种原因无法读取文件,将阻止主线程。

是否有其他人注意到这一点?我可能最终会更改我的设置以使用GCD在后台线程上构建。因为无论何时加载视频都会锁定用户界面。

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; 以异步方式加载内容,但AVPlayerItem似乎没有这样做。

1 个答案:

答案 0 :(得分:0)

也有这个问题。解决了以下问题:

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)
})