美文网首页
exoplayer2的离线下载功能

exoplayer2的离线下载功能

作者: vb12 | 来源:发表于2018-08-08 18:00 被阅读592次

    基本上这两篇博文介绍了离线下载功能的大概

    https://medium.com/google-exoplayer/downloading-streams-6d259eec7f95
    https://medium.com/google-exoplayer/downloading-adaptive-streams-37191f9776e

    在最新的exoplayer2中, 增加了离线下载功能, 现在客户端可以对hls, smooth streaming, dash, 及普通的progressive streams(如mp4)进行离线下载.

    支持的媒体格式:

    hls, smooth streaming, dash, progressive streams

    具体的功能实现基本都在offline目录下.
    基本结构关系:


    image.png

    官方建议通过继承DownloadService的方式来实现下载功能.

    DownloadService

    对于自定义的DownloadService子类, 需要实现下面的方法
    getDownloadManager() 返回一个DownloadManager实例, 这个实例是需要自己创建的, 在创建过程中涉及缓存目录, 缓存文件, 同时下载的最大数目,重试次数, 已经最重要的Deserializer列表
    至于downloaderConstructorHelper, 就是一些媒体源参数的集合.

    downloadManager = DownloadManager(
                        downloaderConstructorHelper,
                        MAX_SIMULTANEOUS_DOWNLOADS,
                        DownloadManager.DEFAULT_MIN_RETRY_COUNT,
                        File(getDownloadDirectory(), DOWNLOAD_ACTION_FILE),
                        *DOWNLOAD_DESERIALIZERS)
    

    getScheduler() 和getRequirements() 通过返回的这两个实例来进行下载策略控制.
    getForegroundNotification() 返回需要在下载时显示的通知, 如果需要前台显示的话.可以通过工具类DownloadNotificationUtil来方便的创建通知对象

    onTaskStateChanged() 在这个方法中判断下载状态, 是否下载完成或者失败.

    DownloadAction

    可以序列化的下载任务, 通过ActionFile来把下载任务保存到文件中, 以及从文件中恢复下载任务.
    对于上面提到的每种支持的格式, 都会有一个对应的DownloadAction实现.
    对于mp4格式的下载, 示例代码如下:

            var uri = Uri.parse(input.text.toString())
            var downloadAction = ProgressiveDownloadAction(uri, false, null, null)
    
            DownloadService.startWithAction(this@MainActivity,
                    DemoDownloadService::class.java, downloadAction, true)
    

    github上放了一份示例

    https://github.com/shaopx/Exoplayer2DownloadTest

    相关文章

      网友评论

          本文标题:exoplayer2的离线下载功能

          本文链接:https://www.haomeiwen.com/subject/lldxbftx.html