Download and Offline Playback
This guide is designed to assist you how to download HLS (HTTP Live Streaming) content using Swift. The DownloadManager will handles the download of HLS content for offline viewing.
You can obtain a basic example of the Player Download and Offline Playback through this sample project.
For basic player SDK integration, you can refer to Getting Started.
Download Manager
Configure the Source
Make sure to set up the source configuration before initiating the download.
guard let sourceConfig = self.sourceConfig else { return }
Initialize Download Manager
Initialize the download manager (DownloadManager) and add it as a listener to receive download updates.
if let manager = try? await DownloadManager.shared.downloadContentManager(for: sourceConfig) {
downloadContentManager = manager
downloadContentManager?.add(listener: self)
trackSelection = try? await downloadContentManager?.fetchAvailableTracks()
} else {
debugPrint("Failed to create offline content manager")
}
Download Status and Monitoring
Handle user interactions to start, pause, resume, cancel, or delete the download.
Download Status
Start Downloads
guard let tracks = trackSelection else { return }
downloadConfig.minimumBitrate = 825_000
downloadContentManager?.download(tracks: tracks, config: downloadConfig)
Pause Downloads
downloadContentManager?.suspendDownload()
Resume Downloads
downloadContentManager?.resumeDownload()
Cancel Downloads
downloadContentManager?.cancelDownload()
Delete Downloads
try? await downloadContentManager?.deleteOfflineData()
Monitoring Download Event
The DownloadContentManagerListener protocol, allowing the view controller to respond to various events related to content download.
func onContentDownloadProgressChanged(_ event: ContentDownloadProgressChangedEvent, manager: DownloadContentManager)
func onContentDownloadFinished(_ event: ContentDownloadFinishedEvent, manager: DownloadContentManager)
func onContentDownloadSuspended(_ event: ContentDownloadSuspendedEvent, manager: DownloadContentManager)
func onContentDownloadResumed(_ event: ContentDownloadResumedEvent, manager: DownloadContentManager)
func onContentDownloadCanceled(_ event: ContentDownloadCanceledEvent, manager: DownloadContentManager)
func onDownloadError(_ event: ContentDownloadErrorEvent, manager: DownloadContentManager)
Offline Playback
Offline Source Config
Represents a SourceConfig which references already downloaded or currently downloading offline content. It can passed to a Player instance for playback.
let offlineSourceConfig = downloadContentManager?.createOfflineSourceConfig()