Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show downloading progress Exoplayer

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
    }
}
like image 205
Jeeva Avatar asked May 16 '20 16:05

Jeeva


People also ask

How do I download ExoPlayer videos on Android?

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.

How do I download Exo player?

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.


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

like image 92
Biscuit Avatar answered Sep 25 '22 21:09

Biscuit