JeffVideoCache

Introduction: Better than AndroidVideoCache
More: Author   ReportBugs   
Tags:

Better than AndroidVideoCache

Refer to the English Development Document

开发文档

  • 1.实现脱离播放器的预加载功能
  • 2.实现视频的边下边播功能
  • 3.实现 M3U8 视频边下边播功能
  • 4.实现 MP4 视频的边下边播功能
  • 5.支持的播放器是 exoplayer 和 ijkplayer
  • 6.支持接入 okhttp
  • 7.支持拖动进度条之后继续缓存到本地的功能
  • 8.支持 LRU 清理规则, 可以设置缓存的过期时间

JeffVideoCache 架构

JeffVideoCache 核心放在客户端, local server 端和 client 做好数据同步的工作

1.如何接入

在 build.gradle 中引入

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

在 demo 中直接引用

dependencies {
    implementation 'com.github.JeffMony:JeffVideoCache:1.0.0'
}
1.1 初始化

程序启动的时候设置 JeffVideoCache ----> SDK 初始化配置

File saveFile = StorageUtils.getVideoFileDir(this);
if (!saveFile.exists()) {
    saveFile.mkdir();
}
VideoProxyCacheManager.Builder builder = new VideoProxyCacheManager.Builder().
        setFilePath(saveFile.getAbsolutePath()).    //缓存存储位置
        setConnTimeOut(60 * 1000).                  //网络连接超时
        setReadTimeOut(60 * 1000).                  //网络读超时
        setExpireTime(2 * 24 * 60 * 60 * 1000).     //2 天的过期时间
        setMaxCacheSize(2 * 1024 * 1024 * 1024);    //2G 的存储上限
VideoProxyCacheManager.getInstance().initProxyConfig(builder.build());

初始化配置:

  • 1.设置缓存存储的路径
  • 2.设置链接超时时间
  • 3.设置网络读超时的时间
  • 4.设置缓存的过期时间
  • 5.设置最大缓存的限制
  • 6.设置自定义的本地代理端口
  • 7.设置是否使用 okhttp;网络请求框架
  • 8.设置网络请求是否忽略证书
1.2 构建本地代理 url
playUrl = ProxyCacheUtils.getProxyUrl(uri.toString(), null, null);

public static String getProxyUrl(String videoUrl, Map<String, String> headers, Map<String, Object> cacheParams)

可以传入 headers, 也可以传入其他额外参数,根据你们自己的需求来

构建的 url 主要是 base64 编码的

1.3 发起请求
VideoProxyCacheManager.getInstance().startRequestVideoInfo(videoUrl, headers, extraParams);

public void startRequestVideoInfo(String videoUrl, Map<String, String> headers, Map<String, Object> extraParams)
1.4 设置缓存监听
VideoProxyCacheManager.getInstance().addCacheListener(videoUrl, mListener);

其中 mListener 如下:

public interface IVideoCacheListener {

    void onCacheStart(VideoCacheInfo cacheInfo);

    void onCacheProgress(VideoCacheInfo cacheInfo);

    void onCacheError(VideoCacheInfo cacheInfo, int errorCode);

    void onCacheForbidden(VideoCacheInfo cacheInfo);

    void onCacheFinished(VideoCacheInfo cacheInfo);
}
1.5 设置正在播放链接
VideoProxyCacheManager.getInstance().setPlayingUrlMd5(ProxyCacheUtils.computeMD5(videoUrl));
1.6 暂停缓存任务
VideoProxyCacheManager.getInstance().pauseCacheTask(mVideoUrl);
1.7 恢复缓存任务
VideoProxyCacheManager.getInstance().resumeCacheTask(mVideoUrl);
1.8 拖动进度条
long totalDuration = mPlayer.getDuration();
if (totalDuration > 0) {
    float percent = position * 1.0f / totalDuration;
    VideoProxyCacheManager.getInstance().seekToCacheTaskFromClient(mVideoUrl, percent);
}
1.9 释放缓存任务
VideoProxyCacheManager.getInstance().stopCacheTask(mVideoUrl);   //停止视频缓存任务
VideoProxyCacheManager.getInstance().releaseProxyReleases(mVideoUrl);

Development Document

  • 1.It can play the video while caching it
  • 2.It supports M3U8 , MP4 and so on
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools