Offline Downloads
We'll explore the workflow in this document.The Sample Android App on Github provides code examples for a typical use case.
Enable Download support
Create TpInitParams with .enableDownloadSupport(true) to enable download support.
var parameters = TpInitParams.Builder()
.setVideoId(videoId)
.setAccessToken(accessToken)
.enableDownloadSupport(true)
.setOfflineLicenseExpireTime(60 * 60 * 25 * 15) //15 Days
.build()
Creating a TpStreamsDownloadManager
The following code snippet demonstrates how to instantiate a TpStreamsDownloadManager
val tpStreamDownloadManager : TpStreamDownloadManager = TpStreamDownloadManager(activityContext)
Using this TpStreamDownloadManager we can get a list of downloaded media and the following media operations to start, delete, pause, resume, and cancel.
Get list of downloaded media
val downloads : LiveData<List<Asset>?> = tpStreamDownloadManager.getAllDownloads()
It will return a list of Asset in LiveData to monitor the download progress use ViewModel and observe.
Start
// Start download Using TpInitParams
tpStreamDownloadManager.startDownload(fragmentActivity, params)
// Start download Using TpStreamPlayer
tpStreamDownloadManager.startDownload(fragmentActivity, player)
Delete
tpStreamDownloadManager.deleteDownload(asset)
Pause
tpStreamDownloadManager.pauseDownload(asset)
Resume
tpStreamDownloadManager.resumeDownload(asset)
Cancel
tpStreamDownloadManager.cancelDownload(asset)
Delete All
tpStreamDownloadManager.deleteAllDownload()
Playing downloaded media
Create offline params and pass them to player activity via intent to play an offline video.
val intent = Intent(activityContext,PlayerActivity::class.java)
intent.putExtra(TP_OFFLINE_PARAMS,TpInitParams.createOfflineParams(video.videoId))
startActivity(intent)