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:
OTVLicenseOptionOffline: Bool
(set to true to persistent the licence).AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: NSNumber
(set the minimum media bitrate).
If this value is set, the bitrate equal to, or the minimum one greater than, this value is selected to download. The highest bitrate is selected by default if it is not set or no proper value is found. For more information, see the Apple AVAssetDownloadTaskMinimumRequiredMediaBitrateKey documentation.AVAssetDownloadTaskMinimumRequiredPresentationSizeKey: NSValue of CGSize
(set the minimum resolution, only available on iOS 14 and above).
If this value is set, the media presentation size equal to, or the minimum one greater than, this value will be selected to download. The highest media presentation size will be selected by default if it is not set or no proper value is found. For more information, see the Apple AVAssetDownloadTaskMinimumRequiredPresentationSizeKey documentation.
The AVAssetDownloadTaskMinimumRequiredMediaBitrateKey
and AVAssetDownloadTaskMinimumRequiredPresentationSizeKey
cannot guarantee the actual download bitrate or resolution will equal the expected value; see Selecting the download stream.
Example code
Setting options to persist the licence
var yourOptions = [OTVLicenseOptionOffline : true]
Setting options to select the minimum bitrate for download
yourOptions[AVAssetDownloadTaskMinimumRequiredMediaBitrateKey] = 600000
Setting options to select the minimum resolution for download
yourOptions[AVAssetDownloadTaskMinimumRequiredPresentationSizeKey] = NSValue(cgSize: CGSize(width: 1280, height: 720))
Starting the download with 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)
}
Pausing a download
OTVPersistenceManager.sharedManager.pauseDownload(asset: OTVPersistenceAsset)
Resuming a download
OTVPersistenceManager.sharedManager.resumeDownload(asset: OTVPersistenceAsset)
Next step: You can play the download.