收听onDownloadComplete Exoplayer

问题描述 投票:0回答:1

我正在尝试下载视频以供在exoplayer中离线播放,但我不知道如何收听onDownloadComplete。在exoplayer文档中,他们说DownloadService是围绕android DownloadManager的包装,因此我尝试收听DownloadManager.ACTION_DOWNLOAD_COMPLETE广播,但它不起作用,实际上这是我第一次使用exoplayer。

下载服务

class MediaDownloadService : DownloadService(
    C.DOWNLOAD_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL,
    C.CHANNEL_ID, R.string.channel_name, R.string.channel_description
) {
    override fun onCreate() {
        registerReceiver(onComplete, IntentFilter(ACTION_DOWNLOAD_COMPLETE))
        super.onCreate()
    }

    override fun onDestroy() {
        unregisterReceiver(onComplete)
        super.onDestroy()
    }

    override fun getDownloadManager(): DownloadManager {
        return DownloadUtil.getDownloadManager(this)
    }

    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
    }


    val onComplete: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(ctxt: Context?, intent: Intent?) {
            toast("Download COmpleted")
        }
    }
}
android kotlin android-download-manager exoplayer2.x
1个回答
0
投票

您可以比较bytesDownloadedcontentLength以检查是否完成下载。

downloadManager.addListener(object : DownloadManager.Listener {
    override fun onDownloadChanged(downloadManager: DownloadManager, download: Download) {
        if (download.bytesDownloaded == download.contentLength) {
            Log.d("Completed")
        }
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.