Starting the download
When the application starts a registered download, it is notified that the download has switched to the .downloading
state. In the .downloading
state, the downloader fetches media segments from the CDN sequentially from the first to the last. When all files of the provided assets have been successfully downloaded, the download state changes to .downloaded
. An OTVPersistenceAsset
in a .downloaded
state would have a valid offlineURL
String property.
Options can be passed into the downloader when calling startDownload()
. The key-value pairs below may be useful:
OTVPrefetchLicense
:Bool
(license already prefetched)AVAssetDownloadTaskMinimumRequiredMediaBitrateKey
:Integer
(set a minimum bitrate)AVAssetDownloadTaskMinimumRequiredPresentationSizeKey
:NSValue of CGSize
(set a minimum resolution, only avilable on iOS 14 and above)
Example code
Persist license
var yourOptions = [OTVPrefetchLicense : true]
Select minimum bitrate for download
yourOptions[AVAssetDownloadTaskMinimumRequiredMediaBitrateKey] = 600000
Select minimm resolution for download
yourOptions[AVAssetDownloadTaskMinimumRequiredMediaBitrateKey] = NSValue(cgSize: CGSize(width: 1280, height: 720))
The following code example shows how to start the download with the yourOptions
func startDownloadAsset(url: URL, assetName: String) -> (Bool, String, OTVPersistenceAsset?) {
// If download a stream encrypted using FPS, create an instance of OTVLicenseDelegate and set licenseDelegate when starting the download.
// E.g. create an instance of OTVSSPLicenseDelegate with FairPlay certificate url and license url with SSP server, and set the stream token for download asset
// If download a clear stream, set licenseDelegate with nil.
let delegate = OTVSSPLicenseDelegate(certificateURL: certificateUrl, licenseURL: licenseUrl)
delegate.setStream(token: streamToken, with url: url)
if let asset = OTVPersistenceManager.sharedManager.startDownload(urlAsset: OTVAVURLAsset(url: url), title: assetName, licenseDelegate: delegate, artwork: nil, options: yourOptions) {
return (true, "", asset)
}
return (false, "download did not start", nil)
}
If required a download may be paused and resumed.
OTVPersistenceManager.sharedManager.pauseDownload(asset: OTVPersistenceAsset)
and
OTVPersistenceManager.sharedManager.resumeDownload(asset: OTVPersistenceAsset)
Next step: You can play the download