【问题标题】:How to show downloading progress ExoplayerExoplayer如何显示下载进度
【发布时间】:2020-05-16 16:42:35
【问题描述】:

我正在尝试在 exoplayer 中下载视频以供离线使用,我想在活动中显示下载进度。

我如何绑定到 exoplayer 中的DownloadService。以便我可以更新活动中的当前下载进度?我尝试覆盖onBind 方法,但没有onBind 方法。

下载服务

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

【问题讨论】:

    标签: android android-download-manager exoplayer2.x


    【解决方案1】:

    在活动中显示当前下载进度

    1. 您需要覆盖并收听DownloadTracker.Listener,这将使您了解下载的不同状态。
    2. 当状态为Download.STATE_DOWNLOADING 时,启动协程/流以将下载的当前值发送到您的活动。 我已经使用了这个流程(每 1 秒发送一次您想要的下载的 percentDownload 的值)
    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. 以您喜欢的方式展示它

    我创建了一个存储库,您可以在其中查看活动中的下载进度,这只是一个可以使用一些优化的示例。

    https://github.com/yoobi/exoplayer-kotlin/tree/master/downloadvideo/src/main/java/io/github/yoobi/downloadvideo

    如果有不清楚的地方,请不要犹豫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      相关资源
      最近更新 更多