I am trying to download a video for offline in exoplayer, I want to show downloading progress inside an activity.
How can I bind to the DownloadService
in exoplayer. so that I can update the current downloading progress in an activity? I try to override onBind
method but there is no onBind
method.
DownloadService
class MediaDownloadService : DownloadService(
C.DOWNLOAD_NOTIFICATION_ID, 1000,
C.CHANNEL_ID, R.string.channel_name, R.string.channel_description
) {
private lateinit var downloadManager: DownloadManager
override fun onCreate() {
downloadManager = DownloadUtil.getDownloadManager(this)
downloadManager.addListener(object : DownloadManager.Listener {
override fun onDownloadChanged(downloadManager: DownloadManager, download: Download) {
if (download.bytesDownloaded == download.contentLength) {
toast("Download Completed!")
}
debug(download.failureReason)
}
})
super.onCreate()
}
override fun getDownloadManager(): DownloadManager {
return downloadManager
}
override fun getForegroundNotification(downloads: MutableList<Download>): Notification {
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationHelper = DownloadNotificationHelper(this, C.CHANNEL_ID)
return notificationHelper.buildProgressNotification(
R.drawable.ic_notification,
pendingIntent,
"simple message",
downloads
)
}
override fun getScheduler(): Scheduler? {
return null
}
inner class DownloadBinder: Binder() {
val service: MediaDownloadService
get() = this@MediaDownloadService
}
}
To add a download you need to create a DownloadRequest and send it to your DownloadService . For adaptive streams DownloadHelper can be used to help build a DownloadRequest , as described further down this page.
ExoPlayer modules can be obtained from JCenter. It's also possible to clone the repository and depend on the modules locally. The easiest way to get started using ExoPlayer is to add it as a gradle dependency. You need to make sure you have the Google and JCenter repositories included in the build.
To show the current download progress in an activity
DownloadTracker.Listener
which will enable you to know the different state of your download.Download.STATE_DOWNLOADING
start a coroutine/flow to send the current value of the download to your activity.
I have used this flow (which send every 1 second the value of the percentDownload of the download you'd like)val downloadManager: DownloadManager // is set before in my object
suspend fun getCurrentProgressDownload(uri: Uri?): Flow<Float?> {
var percent: Float? = downloadManager.currentDownloads.find { it.request.uri == uri }?.percentDownloaded
return callbackFlow {
while(percent != null) {
percent = downloadManager.currentDownloads.find { it.request.uri == uri }?.percentDownloaded
offer(percent)
withContext(Dispatchers.IO) {
delay(1000)
}
}
}
}
I have created a repository where you can see download progress in the activity, This is merely an example that could use some optimisation.
https://github.com/yoobi/exoplayer-kotlin/tree/master/downloadvideo/src/main/java/io/github/yoobi/downloadvideo
if some thing are not clear don't hesitate to ask
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With