Hello,
I'm trying to play a stream video after I downloaded it using a AVAssetDownloadURLSession and aggregateAssetDownloadTask.
When I initialise the AVPlayerItem with the AVURLAsset saved locally in the bookmarks it get the -12865 error. What does this code stands for?
If I play the video directly from the stream it works fine.
This is the code that saves the asset:
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
let userDefaults = UserDefaults.standard
/*
This is the ideal place to begin downloading additional media selections
once the asset itself has finished downloading.
*/
guard let task = task as? AVAggregateAssetDownloadTask,
let asset = activeDownloadsMap.removeValue(forKey: task) else { return }
guard let downloadURL = willDownloadToUrlMap.removeValue(forKey: task) else { return }
// Prepare the basic userInfo dictionary that will be posted as part of our notification.
var userInfo = [String: Any]()
userInfo[Asset.Keys.name] = asset.name
if let error = error as NSError? {
switch (error.domain, error.code) {
case (NSURLErrorDomain, NSURLErrorCancelled):
/*
This task was canceled, you should perform cleanup using the
URL saved from AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didFinishDownloadingTo:).
*/
guard let localFileLocation = localAsset(withName: asset.name)?.urlAsset.url else { return }
do {
try FileManager.default.removeItem(at: localFileLocation)
userDefaults.removeObject(forKey: asset.name)
} catch {
print("An error occured trying to delete the contents on disk for \(asset.name): \(error)")
}
userInfo[Asset.Keys.downloadState] = Asset.DownloadState.notDownloaded.rawValue
case (NSURLErrorDomain, NSURLErrorUnknown):
fatalError("Downloading HLS streams is not supported in the simulator.")
default:
fatalError("An unexpected error occured \(error.domain)")
}
} else {
do {
let bookmark = try downloadURL.bookmarkData()
userDefaults.set(bookmark, forKey: asset.name)
} catch {
print("Failed to create bookmarkData for download URL.")
}
userInfo[Asset.Keys.downloadState] = Asset.DownloadState.downloaded.rawValue
userInfo[Asset.Keys.downloadSelectionDisplayName] = ""
}
NotificationCenter.default.post(name: .AssetDownloadStateChanged, object: nil, userInfo: userInfo)
}
/// Method called when the an aggregate download task determines the location this asset will be downloaded to.
func urlSession(_ session: URLSession, aggregateAssetDownloadTask: AVAggregateAssetDownloadTask,
willDownloadTo location: URL) {
/*
This delegate callback should only be used to save the location URL
somewhere in your application. Any additional work should be done in
`URLSessionTaskDelegate.urlSession(_:task:didCompleteWithError:)`.
*/
willDownloadToUrlMap[aggregateAssetDownloadTask] = location
}
and this one get's the asset back
guard let localFileLocation = userDefaults.value(forKey: name) as? Data else { return nil }
var bookmarkDataIsStale = false
do {
let url = try URL(resolvingBookmarkData: localFileLocation,
bookmarkDataIsStale: &bookmarkDataIsStale)
if bookmarkDataIsStale {
fatalError("Bookmark data is stale!")
}
return AVURLAsset(url: url)
} catch {
fatalError("Failed to create URL from bookmark with error: \(error)")
}
Thank you