google / ExoPlayer

An extensible media player for Android
Apache License 2.0
21.62k stars 6k forks source link

How to use DownloadManager download range data #9986

Open cy41 opened 2 years ago

cy41 commented 2 years ago

Background

In my project, I use ExoPlayer with version 2.11.8 . In this version, I use CacheUtil cache some range data to improve first frame time. sample code like this:

                                            val mp4Url /*a network file uri, it media container type is mp4*/
                                            val upstreamFactory
                                            val simpleCache /*ExoPlayer's SampleCache impl*/

                                            CacheUtil.cache(
                                                    DataSpec(Uri.parse(mp4Url),
                                                            /*position*/ 0,
                                                            /*size*/ 512 * 1024 /*byte*/,
                                                            /*custom key*/ mp4Url),
                                                    simpleCache,
                                                    upstreamFactory,
                                                    { requestLength, bytesCached, _ ->
                                                        val downloadPercentage: Double = (bytesCached * 100.0
                                                                / requestLength)
                                                        if (downloadPercentage == 100.0) {
                                                            Logger.d(TAG, "exo preload finish with 512 KB")
                                                        }
                                                    },
                                                    null
                                            )

In this month, I upgrade ExoPlayer version to 2.15.0 . The "CacheUtil" was removed, I replace it with "CacheWriter". And In other withe, I found a high-level API "DownloadManager", but I don't know how to use it in my project to implement preload or download some range data.

marcbaechinger commented 2 years ago

You are right that CacheUtil has been replaced with CacheWriter with 2.12.0 (see release notes). As far as I know this is a drop-in replacement that offers the same API or at least the same functionality. Let me know if you see issues with that.

I think the partial preloading you are doing still needs to be done programmatically even with the DownloadService and the components that are used by the service. You don't want to create notifications telling the user that some parts are downloaded. That's something you want to do under the hood as you do it already. The DownloadService is used to provide offline functionality rather then technical caching to improve performance when starting a stream.

cy41 commented 2 years ago

You are right that CacheUtil has been replaced with CacheWriter with 2.12.0 (see release notes). As far as I know this is a drop-in replacement that offers the same API or at least the same functionality. Let me know if you see issues with that.

I think the partial preloading you are doing still needs to be done programmatically even with the DownloadService and the components that are used by the service. You don't want to create notifications telling the user that some parts are downloaded. That's something you want to do under the hood as you do it already. The DownloadService is used to provide offline functionality rather then technical caching to improve performance when starting a stream.

Thanks for your reply! My App is a auto play video app, like tiktok. And it run on some low performance device; The device limits the maximum amount of writes the app can write to disk. So my scenes is play current video, and preload next video a little data. And this device limit app can not use Service. I found DownloadManager has some great api to manage preload, but I don't know how to use it like use CacheWriter.

marcbaechinger commented 2 years ago

I don't understand how using the DownloadManager would help with the limited writes on disc on these devices. Under the hood, the DownloadManager is using CahceWriter as well. So directly using ther CacheWriter is not an issue I think.