在使用 AVContentSessionKey 的 FairPlay Streaming Server SDK v4.0.1 中的演示之后,我正在使用 HLS Fairplay 实现离线播放。我下载了三个内容,每个内容都正确下载并保存,文件目录上的.movpkg及其内容键,当我关闭WIFI时,这三个下载的内容可以正常播放,没有任何问题,在播放之前我使用这个代码:
let urlAsset = element.urlAsset!
ContentKeyManager.shared.contentKeySession.addContentKeyRecipient(urlAsset)
if !urlAsset.resourceLoader.preloadsEligibleContentKeys {
urlAsset.resourceLoader.preloadsEligibleContentKeys = true
}
self.present(playerViewController, animated: true, completion: {
AssetPlaybackManager.sharedManager.setAssetForPlayback(urlAsset)
})
到目前为止,一切都很好。但问题是当我关闭应用程序(主页按钮关闭应用程序)然后播放下载的内容时,只有最后下载的内容正确播放,其他内容(第一个和第二个)在控制台上发送这些错误。
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed"
UserInfo={NSUnderlyingError=0x1c065d760 {Error Domain=NSOSStatusErrorDomain Code=-16227 "(null)"},
NSLocalizedFailureReason=An unknown error occurred (-16227),
NSURL=file:///private/var/mobile/Containers/Data/Application/A950D8DB-B711-47E3-AAF5-C95CC9682430/Library/com.apple.UserManagedAssets.kkG8Ih/644986_7798B8476A473F68.movpkg/, NSLocalizedDescription=The operation could not be completed}
我用文档目录中的键仔细检查 .movpkg 并正确显示
/Documents/.keys/one-key
/Documents/.keys/two-key
/Documents/.keys/three-key
在错误发生之前,调用 ContentKeyDelegate 并正确加载密钥并将其传递给请求。
如果persistableContentKeyExistsOnDisk(withContentKeyIdentifier:assetIDString){
let urlToPersistableKey = urlForPersistableContentKey(withContentKeyIdentifier: assetIDString)
guard let contentKey = FileManager.default.contents(atPath: urlToPersistableKey.path) else {
/
pendingPersistableContentKeyIdentifiers.remove(assetIDString)
return
}
/
Create an AVContentKeyResponse from the persistent key data to use for requesting a key for
decrypting content.
*/
let keyResponse = AVContentKeyResponse(fairPlayStreamingKeyResponseData: contentKey)
/
keyRequest.processContentKeyResponse(keyResponse)
return
}
如果我打印 contentKeyRecipients 三个内容正确显示
- (lldb) po
ContentKeyManager.shared.contentKeySession.contentKeyRecipients ▿ 3
elements
- 0 : AVURLAsset: 0x1c0234d40, URL = file:///private/var/mobile/Containers/Data/Application/E791A4DE-4261-46B7-A84D-D10B27035FAE/Library/com.apple.UserManagedAssets.kkG8Ih/539628_20469336224AA388.movpkg
- 1 : AVURLAsset: 0x1c0234fa0, URL = file:///private/var/mobile/Containers/Data/Application/E791A4DE-4261-46B7-A84D-D10B27035FAE/Library/com.apple.UserManagedAssets.kkG8Ih/644986_7798B8476A473F68.movpkg
- 2 : AVURLAsset: 0x1c42391c0, URL = file:///private/var/mobile/Containers/Data/Application/E791A4DE-4261-46B7-A84D-D10B27035FAE/Library/com.apple.UserManagedAssets.kkG8Ih/573744_62377F9549C45B93.movpkg
我的测试在 iOS 11.1.2 和 iOS 11.2 beta 2 中
我不确定发生了什么,但似乎是持久密钥的问题,我不知道是否每个内容都需要与一个 AVContentKeySession 相关联。
如果有人遇到类似的问题,任何帮助将不胜感激。
提前致谢