How to show downloading progress Exoplayer

Viewed 1169

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
    }
}
1 Answers

To show the current download progress in an activity

  1. You need to override and listen to the DownloadTracker.Listener which will enable you to know the different state of your download.
  2. When the state is at 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)
            }
        }
    }
}
  1. Display it the way you like

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

Related